shiika
shiika copied to clipboard
impl. `return` from a block
eg.
class A
def self.foo
[1,2,3].each do |i: Int|
p i
return if i %2 == 0
end
p "unreachable" # this should not be printed
end
end
On the other hand, I feel like the return
below escapes from fn
, not self.foo
. Maybe return
should behave differently in a fn
and a block (like Ruby's lambda and proc.)
class A
def self.foo
f = fn{
[1,2,3].each do |i: Int|
p i
return if i %2 == 0
end
}
3.times{|i: Int| f.call}
end
end
This is possible but not easy (needs something like exception handlling)
Another possibility is to use CPS(Continuation Passing Style).