Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • May 16 19:05
    lbguilherme synchronize #11947
  • May 16 17:29
    straight-shoota milestoned #11971
  • May 16 17:15
    straight-shoota closed #12038
  • May 16 17:15
    straight-shoota closed #12036
  • May 16 17:14
    straight-shoota edited #12038
  • May 16 13:47
    straight-shoota closed #12051
  • May 16 13:47
    straight-shoota closed #12050
  • May 16 12:17
    beta-ziliani edited #7318
  • May 15 08:43
    r00ster91 closed #7318
  • May 15 08:17
    r00ster91 closed #7690
  • May 14 17:01
    Blacksmoke16 labeled #11995
  • May 14 13:03
    webhive closed #12055
  • May 14 10:19
    straight-shoota milestoned #12051
  • May 14 10:00
    I3oris edited #12051
  • May 14 08:45
    oprypin review_requested #11977
  • May 13 23:29
    Blacksmoke16 unlabeled #12061
  • May 13 22:56
    Blacksmoke16 edited #12061
  • May 13 22:56
    Blacksmoke16 edited #12061
  • May 13 22:54
    Blacksmoke16 labeled #12061
  • May 13 22:54
    Blacksmoke16 labeled #12061
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.
mfiano
@mjfiano:matrix.org
[m]
Ah
From IRC (bridge bot)
@FromIRC
<riza> @suskeyhose the semantics of the break operator in crystal (and ruby) are a bit counterintuitive because of the way that loops are generally implemented -- as blocks
<riza> this is also true of next
srasu
@suskeyhose:matrix.org
[m]
That answered my question
mfiano
@mjfiano:matrix.org
[m]
I'm guessing I need a macro to make this generic using Indexable instead of x/y/z/w in order to make a mixin that can be added to 2d, 3d and 4d vectors? The short-circuiting behavior of && tells me yes
    def <=>(other : self)
      @x <=> other.x && @y <=> other.y
    end
I'm just hoping macros are expanded after type merging
From IRC (bridge bot)
@FromIRC
<riza> make it generic meaning accept any number of operands to that comparison? like to be able to compare some XY with another XY and some XYZ with another XYZ, etc?
mfiano
@mjfiano:matrix.org
[m]
Make it generic as in, using each or something from Indexable rather than the logic explicitly needing to know about the ivars
But I think we meant the same thing
From IRC (bridge bot)
@FromIRC
<riza> you could either: ditch individual ivars and have an array which represented the set -- you could do this with generics easily
<riza> or you can use a macro to build out the guts of the class
Ary Borenszweig
@asterite
@suskeyhose:matrix.org you can break from a block where there's no while at all. The break goes a GOTO to the end of the called method (done with LLVM)
mfiano
@mjfiano:matrix.org
[m]
Thanks
From IRC (bridge bot)
@FromIRC
George Dietrich
@Blacksmoke16
:grimacing:
im sure there's a better way...
From IRC (bridge bot)
@FromIRC
<raz> i wish... but all these things can be nil and i can't change it (protobuf). i tried rewriting it in a bunch of ways. but it seems my only choices are spreading it out over 6 variable assignments or, well, this.