Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 14:51
    HertzDevil synchronize #12362
  • 14:50
    HertzDevil closed #10571
  • 14:49
    HertzDevil closed #11825
  • 14:47
    straight-shoota milestoned #11420
  • 14:32
    straight-shoota closed #11954
  • 14:32
    straight-shoota closed #12368
  • 14:31
    straight-shoota closed #5044
  • 14:31
    straight-shoota closed #10954
  • 14:29
    straight-shoota milestoned #12388
  • 14:28
    straight-shoota milestoned #12387
  • 14:18
    straight-shoota synchronize #11420
  • 14:15
    straight-shoota milestoned #12356
  • 14:15
    straight-shoota milestoned #12385
  • 14:12
    straight-shoota milestoned #12384
  • 12:27
    asterite labeled #12388
  • 12:27
    asterite labeled #12388
  • 12:26
    asterite opened #12388
  • 12:10
    asterite synchronize #12375
  • 11:56
    asterite opened #12387
  • 11:56
    asterite labeled #12387
mfiano
@mjfiano:matrix.org
[m]
I am not that familiar with generics in Crystal, and I'm wondering if they are similar to Nim or Rust in that I can restrict a generic type parameter inline. The following is a Nim function definition, where T is restricted to be of type Vec, which is used for both arguments and the return type. How can I do similar with Crystal, without a type alias or relying on the inferred call site types?
proc abs[T: Vec](o: var T, v: T): var T = ...
George Dietrich
@Blacksmoke16
not in that way, would have to be within one of the methods in a macro
i.e. not something the lang supports atm
mfiano
@mjfiano:matrix.org
[m]
I see, thanks.
George Dietrich
@Blacksmoke16
From IRC (bridge bot)
@FromIRC
<straight-shoota> This would be an example of a macro-based type restriction: https://github.com/crystal-lang/crystal/blob/13dd77ef01c0d55f2d43372f97c648ff81c737d9/src/slice.cr#L745-L748
Ary Borenszweig
@asterite
another way would be to use a helper method: def foo(x : T, v : T) : T; foo_helper(x, v); end; def foo_helper(x : Vec, v : Vec). So both have to be the same type, and both have to be Vec
mfiano
@mjfiano:matrix.org
[m]
Yeah I'm not a fan of making the body less readable to restrict a type or two
Also not realy a fan of Nim's inline restriction...would like a more readable where or even overloading forall
George Dietrich
@Blacksmoke16
to be clear you could put that 1 line macro in the constructor and thats all you'd need
mfiano
@mjfiano:matrix.org
[m]
I think I want to see the information in the signature where it belongs
From IRC (bridge bot)
@FromIRC
<straight-shoota> one day we'll hopefully get that =)
George Dietrich
@Blacksmoke16
yea thats not a thing atm, but can emulate it yourself for 1 line
so :shrug: could be worse
Ary Borenszweig
@asterite
Yes, it's definitely not hard to implement, we just need to come up with a syntax and do it :-)
mfiano
@mjfiano:matrix.org
[m]
:) Sure, not a deal breaker for me. I like Crystal's "for humans" philosophy, and adding line noise is not in line with that :)
Regardless, it is one thing I would use a lot if it was a thing.
I might play around with a macro sometime...for now it's not really needed. Thanks!
mfiano
@mjfiano:matrix.org
[m]
@Blacksmoke16: Where is out used, or is it just a reserved keyword?
mfiano
@mjfiano:matrix.org
[m]
Aha
I thought about your macro generating the entire def instead of mine just generating the body, and I decided on mine, for a couple reasons. 1) The readability as mentioned. 2) I would like to selectively choose which ones to annotate as always inlined, 3) I would like to add additional logic to some methods.
mfiano
@mjfiano:matrix.org
[m]

I'm trying to write a class method that initializes an Indexable type with random floats.

    def self.rand(min = 0.0, max = 1.0)
      res = new
      res.each_index { |i| res[i] = Random.rand min..max }
      res
    end

This works, but I am wondering if this can be done more concisely/in one line instead of 3. Don't really want to macro around this if the language has something I could use that I don't know about.

From IRC (bridge bot)
@FromIRC
<straight-shoota> I don't think this is much about the language, more about the API of your type
mfiano
@mjfiano:matrix.org
[m]
Perhaps.
mfiano
@mjfiano:matrix.org
[m]
I am thinking you are implying I write a new that takes a block. That would help at least.
From IRC (bridge bot)
@FromIRC
<riza> why not just put it in the initialize method?
From IRC (bridge bot)
@FromIRC
George Dietrich
@Blacksmoke16
its prob not in shardbox
its a manual registry remember
From IRC (bridge bot)
@FromIRC
<hightower2> ah so, didn't remember offhand, thanks
<hightower2> shards.info finds it, but also only if trying by 'tasko.cr' rather than just 'tasko'
srasu
@suskeyhose:matrix.org
[m]
Does the break statement break from the first while in its dynamic scope? Or are blocks handled specially by inlining to allow loop to have a block with a break statement?
George Dietrich
@Blacksmoke16
not sure, would have to test it
srasu
@suskeyhose:matrix.org
[m]
If you're curious enough to test it ping me when you do, otherwise I'll say here when I have a compiler on hand.
From IRC (bridge bot)
@FromIRC
<riza> @suskeyhose check out https://carc.in and test it right now!
Ary Borenszweig
@asterite
@suskeyhose:matrix.org break breaks from the closest block or while/until, whichever comes first
Emilia
@rymiel:rymiel.space
[m]
Didn't expect my tiny compile time warning patch to get a heart from Müller 😮
srasu
@suskeyhose:matrix.org
[m]

I'm curious how loop works then. In code like

loop do
  break
end

wouldn't the break exit the yield block and then just continue the loop?

It seems like this would work more like skip than break
ryanprior
@ryanprior:matrix.org
[m]
srasu: you might be thinking of next. When you break its argument (or nil) becomes the value of the whole loop expression.
srasu
@suskeyhose:matrix.org
[m]
Right, I guess I was just curious how this deals with environments where the break isn't in the lexical scope of a while directly, but is only there via (potentially several layers of) blocks
ryanprior
@ryanprior:matrix.org
[m]
I think maybe loop is a macro that expands to a while? Not a hundred on that
mfiano
@mjfiano:matrix.org
[m]
riza: That is one thing that confuses me, when to use initialize vs new, despite reading the docs and fully understanding what is written.
srasu
@suskeyhose:matrix.org
[m]
it's like C++ placement new vs new. allocate is like malloc(sizeof(T)), then you call initialize to make it valid. If you call new it does both for you.
mfiano
@mjfiano:matrix.org
[m]
I'm sorry but I was smart and chose to ignore C++ as much as I could after C :)
Or rather, when to define an initialize with a block as opposed to new
srasu
@suskeyhose:matrix.org
[m]
Oh, then for C it's like this:
void x_initialize(X *x) {
    // set all the stuff in x, which was uninit memory
}

X *x_new() {
    X *x = malloc(sizeof(X));
    x_initialize(x);
    return x;
}
Since crystal auto-defines new to do this for you, you should only define your own new if you need to change that malloc call to allocate your structure differently.