Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 06:02
    HertzDevil labeled #11546
  • 06:02
    HertzDevil labeled #11546
  • 06:02
    HertzDevil opened #11546
  • 05:20
    HertzDevil synchronize #11379
  • 03:29
    HertzDevil review_requested #11524
  • 03:28
    HertzDevil edited #11379
  • 03:28
    HertzDevil edited #11379
  • 03:27
    HertzDevil synchronize #11379
  • 03:09
    HertzDevil edited #11545
  • 03:08
    HertzDevil edited #11545
  • 03:05
    HertzDevil edited #11545
  • 03:00
    HertzDevil labeled #11545
  • 03:00
    HertzDevil labeled #11545
  • 03:00
    HertzDevil labeled #11545
  • 03:00
    HertzDevil labeled #11545
  • 03:00
    HertzDevil opened #11545
  • 02:44
    neatorobito synchronize #11543
  • 00:57
    oprypin review_requested #11543
  • Dec 06 23:49
    BlobCodes synchronize #11094
  • Dec 06 23:47
    straight-shoota milestoned #11230
George Dietrich
@Blacksmoke16
even if you have a helper private method its still just boilerplate
mfiano
@mjfiano:matrix.org
[m]
I'm feeling a little intimidated by Crystal macros still
Spent last night trying to figure out the basics from reading the docs...couldn't apply it to my own code
George Dietrich
@Blacksmoke16
{% for m in ["round", "abs"] %}
    def {{m.id}}
      v = dup
      v.each_with_index { |elt, i| v[i] = elt.{{m.id}} }
      v
    end
{% end %}
assumes the method name is the same as the method called on the element, but could easily work around that if thats not what you want
mfiano
@mjfiano:matrix.org
[m]
In my case, I'd only want to generate the method body, not the whole method, supplying the name of the method called on the element
and therefor without a hard-coded list of strings too
George Dietrich
@Blacksmoke16
why would it matter?
mfiano
@mjfiano:matrix.org
[m]
@Blacksmoke16: I took your advice 😀
George Dietrich
@Blacksmoke16
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
George Dietrich
@Blacksmoke16
hmm
i have an idea
mfiano
@mjfiano:matrix.org
[m]
This is intended to be included on all 3 of my vector types. I may split it up into 2 mixins, for the methods that don't make sense for matrices
George Dietrich
@Blacksmoke16
private macro element_wise(name, &)
  def {{name.id}}
    out = dup
    each_with_index { |elt, idx| out[idx] = {{yield}} }
    out
  end
end

element_wise fract do
  elt - elt.floor
end
I think that should work
or even element_wise frac { elt - elt.floor }
mfiano
@mjfiano:matrix.org
[m]
Idea is to try to get rid of all references to @x, @y, etc so it's DRY for the related types
Oh, lemme read
George Dietrich
@Blacksmoke16
also fwiw out is a keyword so prob wouldn't be a bad idea to pick something else
technically works, but going to mess up highlighting
mfiano
@mjfiano:matrix.org
[m]
I mean, that doesn't save any code by expanding into a toplevel definition, and imho isn't as easy to scan the file
George Dietrich
@Blacksmoke16
:thinking: how do you figure that doesn't save any code?
mfiano
@mjfiano:matrix.org
[m]
re: out: ok
9-11 are 3 lines. so is 24-26 in mine?
George Dietrich
@Blacksmoke16
  def sign
    element_wise elt.sign.to_f
  end

  def copysign(other : self)
    element_wise Math.copysign elt, other[idx]
  end

  def fract
    element_wise elt - elt.floor
  end
Gets turned into:
element_wise sign { elt.sign.to_f }
element_wise copysign { Math.copysign elt, other[idx] }
element_wise fract { elt - elt.floor }
mfiano
@mjfiano:matrix.org
[m]
I could just as well do that with mine since they're short, albeit a bit differently
The benefit is I instantly know they are defs, and syntax highlighting gives my brain less to process when quickly scanning a source file
Correct me if I'm wrong :)
George Dietrich
@Blacksmoke16
fair enough
mfiano
@mjfiano:matrix.org
[m]
But I'll think about it. You have given me nothing but good suggestions this week and I am a bit tired now and about to quit, so we'll see. Thank you
George Dietrich
@Blacksmoke16
o/
on an unrelated note, https://play.crystal-lang.org/#/r/blio is this output expected?
looks to be a regression in 0.31.0
Matthew Berry
@mattrberry
Out of curiosity, are there downsides to a type system that supports something like this? https://carc.in/#/r/bljn
Quinton Miller
@HertzDevil
From IRC (bridge bot)
@FromIRC
<raz> if foo = bar should be a syntax error
From IRC (bridge bot)
@FromIRC
<raz> i keep seeing it in crystal code used as a workaround for .try and .not_nil!. people want their code to be fluent to read, but replacing warts with traps is not a solution :⁠(
Quinton Miller
@HertzDevil
they are fluent to read unless you come from c
even in c++ if (auto y = dynamic_cast<Derived *>(x)) is perfectly fine and fluent
From IRC (bridge bot)
@FromIRC
<raz> in many (most?) languages assignment in conditional is a compiler warning or even error. and i think for good reason.
<raz> no, that's a warning C4706
Quinton Miller
@HertzDevil
and in many, they aren't
also for good reasons
c++ compilers are not required to emit that warning so it's not the language that's responsible here
you could see if ameba warns about that, but crystal itself should not disallow if foo = bar
From IRC (bridge bot)
@FromIRC
<raz> i think the bigger problem is that crystal not only allows but even encourages it
Quinton Miller
@HertzDevil
if you think that's a bigger problem you could try to replace all occurrences in the standard library plus the compiler and see how many others think disallowing that is the job of the compiler and not a linter
From IRC (bridge bot)
@FromIRC
<raz> hm, i think it relates to designing for experts vs mortals. of course it's not a problem for compiler & stdlib authors. they intuitively structure their code to avoid nil to begin with. the mortals (like me) are often not that smart. so they more frequently end in a situation where they have to choose between ugly (not_nil / try), trappy (if =) or going back and refactoring all the things.
<raz> it compounds because shards and external data often also don't avoid nil as much as they should/could.