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/
where
or even overloading forall
signature
where it belongs
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.
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.
new
that takes a block. That would help at least.
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?
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?
initialize
vs new
, despite reading the docs and fully understanding what is written.
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