The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
initialize
with a block as opposed to new
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;
}
def initialize(@x, @y); end
def self.new(value : T = 0.0)
new value, value
end
def initialize(@x, @y); end
def initialize(value = 0.0)
initialize(value, value)
end
initialize
from now on unless I need to change the allocation strategy.
new
method that prompted this question early today? def self.new(&)
res = new
res.each_index { |i| res[i] = yield i }
res
end
Indexable
I do this to allow passing a block
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
include
vs extend
?
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"?
next
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
each
or something from Indexable
rather than the logic explicitly needing to know about the ivars