Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 08:54
    straight-shoota closed #12158
  • 08:54
    straight-shoota closed #12141
  • 08:54
    straight-shoota closed #12123
  • 08:54
    straight-shoota closed #3835
  • 08:54
    straight-shoota closed #12094
  • 08:54
    straight-shoota closed #12061
  • Jun 27 19:37
    asterite synchronize #12165
  • Jun 27 19:34
    asterite synchronize #12165
  • Jun 27 17:55
    beta-ziliani milestoned #12149
  • Jun 27 12:32
    Blacksmoke16 unlabeled #12165
  • Jun 27 12:32
    Blacksmoke16 labeled #12165
  • Jun 27 12:32
    Blacksmoke16 labeled #12165
  • Jun 27 12:32
    Blacksmoke16 labeled #12165
  • Jun 27 11:56
    asterite opened #12165
  • Jun 26 21:23
    straight-shoota milestoned #12163
  • Jun 26 21:23
    straight-shoota edited #12163
  • Jun 26 21:18
    straight-shoota milestoned #11773
  • Jun 26 21:18
    straight-shoota milestoned #12008
  • Jun 26 21:15
    straight-shoota unlabeled #5659
  • Jun 26 21:12
    straight-shoota milestoned #11872
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.
mfiano
@mjfiano:matrix.org
[m]
I'll think about this. Currently I do the following to initialize my ivars for my vec2, and also allow 0-1 arity construction:
    def initialize(@x, @y); end

    def self.new(value : T = 0.0)
      new value, value
    end
From IRC (bridge bot)
@FromIRC
<riza> overloaded constructors are one of the few places I've cared to redefine new, but for most everyday purposes #initialize is sufficient
srasu
@suskeyhose:matrix.org
[m]
how about
def initialize(@x, @y); end
def initialize(value = 0.0)
  initialize(value, value)
end
mfiano
@mjfiano:matrix.org
[m]
Good to know I'm not alone then
From IRC (bridge bot)
@FromIRC
<riza> you can easily just declare #initialize multiple times too for the different aritys
<riza> yeah, like @suskeyhose's example
George Dietrich
@Blacksmoke16
i usually use .new as a means to transform arguments to match the initializer
mfiano
@mjfiano:matrix.org
[m]
Can you give an example within the current context of when you should define new, or does it not apply in any way you can think here?
George Dietrich
@Blacksmoke16
def self.new(value = 0.0)
  new value, value
end

def initialize(@x, @y); end
srasu
@suskeyhose:matrix.org
[m]
As an absolute newbie with no knowledge of the allocation scheme and only reading what I have in the reference docs, I'd prefer only redefining self.new for changes to the allocation scheme, doing overrides for initialize, and if absolutely needed making a self.of method for alternate construction methods which use the same allocation scheme.
From IRC (bridge bot)
@FromIRC
<riza> there are certainly performance considerations with allocating and such ahead of time, I just never need that
srasu
@suskeyhose:matrix.org
[m]
You mean like making object pools and then overriding new and finalize to just mess with the object pool?
mfiano
@mjfiano:matrix.org
[m]
Ok, I see. I will switch to overriding initialize from now on unless I need to change the allocation strategy.
THat makes the most sense to me now that I understand it better.
srasu
@suskeyhose:matrix.org
[m]
👍
mfiano
@mjfiano:matrix.org
[m]
I guess I defined new for extra constructors because it mapped better to my brain, probably because it is a class method.
The specialness of initialize confused me
srasu
@suskeyhose:matrix.org
[m]
That's fair.
mfiano
@mjfiano:matrix.org
[m]
What about the new method that prompted this question early today?
    def self.new(&)
      res = new
      res.each_index { |i| res[i] = yield i }
      res
    end
Since my struct is Indexable I do this to allow passing a block
George Dietrich
@Blacksmoke16
thats probably fine
mfiano
@mjfiano:matrix.org
[m]
Example usages:
  def self.rand(min : T = 0.0, max : T = 1.0)
    new { Random.rand min..max }
  end

  def self.rand(min : self = ZERO, max : self = ONES)
    new { |i| Random.rand min[i]..max[i] }
  end
srasu
@suskeyhose:matrix.org
[m]
Yeah, that makes sense to me if Crystal doesn't provide a way to pass a block via initialize normally
mfiano
@mjfiano:matrix.org
[m]
That I'm not sure of, but thank you!
mfiano
@mjfiano:matrix.org
[m]
Anyone ever wish there was a way to mixin a module with instance and class methods and it doing the logical thing, rather than choosing how they are converted with include vs extend?
srasu
@suskeyhose:matrix.org
[m]
I finally see the answer to my question. A break inside a block breaks from the method that called the block

Which makes me curious about this

def something
  yield
end

def pass
  something { yield }
  puts "hey"
end

pass do
  break
end

does it print "hey"?

From IRC (bridge bot)
@FromIRC
<riza> @mjfiano yeah, that's the exact situation I was referring to when I said I use new sometimes to overload.