Make alias parameter optional?
Jacob Carlborg
doob at me.com
Mon Feb 27 00:56:36 PST 2012
On 2012-02-27 00:04, Ali Çehreli wrote:
> On 02/26/2012 03:45 AM, Jacob Carlborg wrote:
> > On 2012-02-26 11:03, Ali Çehreli wrote:
> >> On 02/25/2012 05:04 PM, Robert Rouse wrote:
> >> > On Saturday, 25 February 2012 at 23:10:51 UTC, Ary Manzana wrote:
> >> >> On 2/25/12 7:31 PM, Robert Rouse wrote:
> >>
> >> ...
> >>
> >> >>> This means that D can simulate Ruby blocks more than I thought.
> >> That's
> >> >>> pretty awesome. I'm loving D more every day.
> >> >>
> >> >> How's that like a Ruby block?
> >> >
> >> > The D code simulates the following Ruby if you were to make bar print
> >> > "something" with writeln.
> >> >
> >> > def foo(a, b, &block)
> >> > puts "a is #{a}")
> >> > b.call
> >> > yield
> >> > end
> >> >
> >> > f = lambda { puts "good bye" }
> >> >
> >> > foo(1, f) { puts "something" }
> >> >
> >> >
> >> > That's what I'm talking about.
> >> >
> >>
> >> I don't know Ruby but from what I've read so far about Ruby blocks,
> >> their D equivalents may also be D ranges.
> >>
> >> Ali
> >
> > A Ruby block is basically like a delegate in D and has nothing to do
> > with ranges.
> >
> > Ruby:
> >
> > def foo (&block)
> > block.call
> > end
> >
> > foo do
> > p "asd"
> > end
> >
> > D:
> >
> > void foo (void delegate () block)
> > {
> > block();
> > }
> >
> > void main ()
> > {
> > foo({
> > writeln("asd");
> > });
> >
> > foo(() => writeln("asd")); // new lambda syntax
> > }
> >
> > Both examples print "asd". If you want to have a more Ruby looking
> > syntax in D you do some operator overload abuse:
> >
> > struct Block
> > {
> > void delegate (void delegate ()) impl;
> >
> > void opIn (void delegate () block)
> > {
> > impl(block);
> > }
> > }
> >
> > Block foo ()
> > {
> > return Block((x) => x());
> > }
> >
> > void main ()
> > {
> > foo in {
> > writeln("asd");
> > };
> > }
> >
>
> I see, thanks. The reason I thought about ranges is that the yield
> statement in the Ruby code above reminded me of Python's generators, and
> that D's ranges can also take the role of generators.
>
> Ali
>
"yield" in Ruby is just a way to call a block:
def foo
yield
end
def bar (&block)
block.call
end
The second example is a more explicit way of calling a block. Often
blocks are used to iterate in Ruby:
[3, 4, 5].each do |e|
# do something with e
end
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list