Impressed

Jacob Carlborg doob at me.com
Fri Jul 27 01:11:20 PDT 2012


On 2012-07-27 09:42, Nick Sabalausky wrote:

> I wonder how that works under the hood. Like, if it's somewhat similar
> to opApply. Or maybe more like an Asm-style "messin' with the call
> stack's return addresses". I would hope it wouldn't involve fiber
> context switches, unless they're somehow much faster than D fiber
> context switches (which would seem weird, being in a VM).

I have no idea. But Ruby uses a similar approach:

def foo
   yield 1
   yield 2
   yield 3
end

foo do |e|
   puts e
end

Results in:

1
2
3

But in Ruby that's basically syntax sugar for:

def foo (&block)
   block.call(1)
   block.call(1)
   block.call(1)
end

Which in D would be the same as:

void foo (void delegate (int) dg)
{
     dg(1);
     dg(2);
     dg(3);
}

foo((e){
     writeln(e);
});

Which is basically how opApply works.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list