On Thursday, 31 October 2013 at 20:56:11 UTC, Peter Alexander
wrote:
On Thursday, 31 October 2013 at 07:41:30 UTC, Jacob Carlborg
wrote:
I use Ruby all day with a lot of blocks (closures) and I never
had any problem. In Ruby everything is an object and passed
around by reference. I guess that's help.
BTW, the default iteration pattern in Ruby is to use blocks:
[1, 2, 3].each { |e| puts e }
Closest translation in D:
[1, 2, 3].each!(e => writeln(e));
But in D one would of course use a foreach loop instead.
Those aren't closures, it captures none of the environment.
Those are just simple lambda functions.
Maybe this would be a better Ruby example?
##################################################
module App
def self.run
register_callbacks
Other.call_all
end
def self.register_callbacks
foo = 10
Other.register { puts foo }
Other.register { puts self }
foo = 20
Other.register { puts foo }
foo = 30
end
end
module Other
@@callbacks = []
def self.register (&blk)
@@callbacks << blk
end
def self.call_all
@@callbacks.each {|cb| cb.call }
end
end
App.run if __FILE__ == $0
##################################################
Output is:
30
App
30
-- Chris NS