View mode: basic / threaded / horizontal-split · Log in · Help
February 25, 2012
Make alias parameter optional?
Is it possible to do something like this?

void foo(T, T2, alias thing)(T a, T2 b) {
  // do stuff with a
  // call b (since b would be a delegate)
  // call thing if thing is given
}

I come from the Ruby world and I'm just playing around to see how 
much I can replicate of the "block" functionality that Ruby has.
February 25, 2012
Re: Make alias parameter optional?
void foo(T, T2, alias thing = (){})(T a, T2 b)
{
	thing();
}

void bar(){}

void main()
{
	foo!(int,int,bar)(1,2);
	foo(1,2);
}
February 25, 2012
Re: Make alias parameter optional?
On Saturday, 25 February 2012 at 18:54:35 UTC, Trass3r wrote:
> void foo(T, T2, alias thing = (){})(T a, T2 b)
> {
> 	thing();
> }
>
> void bar(){}
>
> void main()
> {
> 	foo!(int,int,bar)(1,2);
> 	foo(1,2);
> }

Cool. Didn't know you can do that, but I guess it makes sense 
that it would work that way.

The only thing I wish for is if I didn't have to explicitly 
define what T and T2 were and I could just do

foo!(bar)(1,2);
February 25, 2012
Re: Make alias parameter optional?
On 02/25/2012 01:55 PM, Robert Rouse wrote:
> On Saturday, 25 February 2012 at 18:54:35 UTC, Trass3r wrote:
>> void foo(T, T2, alias thing = (){})(T a, T2 b)
>> {
>> thing();
>> }
>>
>> void bar(){}
>>
>> void main()
>> {
>> foo!(int,int,bar)(1,2);
>> foo(1,2);
>> }
>
> Cool. Didn't know you can do that, but I guess it makes sense that it
> would work that way.
>
> The only thing I wish for is if I didn't have to explicitly define what
> T and T2 were and I could just do
>
> foo!(bar)(1,2);

The following works and is news to me. Apparently template parameters 
with default values need not be at the end of the template parameter list:

void foo(alias thing = (){}, T, T2)(T a, T2 b)
{
    thing();
}

void bar(){}

void main()
{
    foo!(bar)(1,2);
}

Ali
February 25, 2012
Re: Make alias parameter optional?
On Saturday, 25 February 2012 at 22:12:55 UTC, Ali Çehreli wrote:
> On 02/25/2012 01:55 PM, Robert Rouse wrote:
>> On Saturday, 25 February 2012 at 18:54:35 UTC, Trass3r wrote:
>>> void foo(T, T2, alias thing = (){})(T a, T2 b)
>>> {
>>> thing();
>>> }
>>>
>>> void bar(){}
>>>
>>> void main()
>>> {
>>> foo!(int,int,bar)(1,2);
>>> foo(1,2);
>>> }
>>
>> Cool. Didn't know you can do that, but I guess it makes sense 
>> that it
>> would work that way.
>>
>> The only thing I wish for is if I didn't have to explicitly 
>> define what
>> T and T2 were and I could just do
>>
>> foo!(bar)(1,2);
>
> The following works and is news to me. Apparently template 
> parameters with default values need not be at the end of the 
> template parameter list:
>
> void foo(alias thing = (){}, T, T2)(T a, T2 b)
> {
>     thing();
> }
>
> void bar(){}
>
> void main()
> {
>     foo!(bar)(1,2);
> }
>
> Ali

This means that D can simulate Ruby blocks more than I thought. 
That's pretty awesome. I'm loving D more every day.
February 25, 2012
Re: Make alias parameter optional?
On 2/25/12 7:31 PM, Robert Rouse wrote:
>
>
> On Saturday, 25 February 2012 at 22:12:55 UTC, Ali Çehreli wrote:
>> On 02/25/2012 01:55 PM, Robert Rouse wrote:
>>> On Saturday, 25 February 2012 at 18:54:35 UTC, Trass3r wrote:
>>>> void foo(T, T2, alias thing = (){})(T a, T2 b)
>>>> {
>>>> thing();
>>>> }
>>>>
>>>> void bar(){}
>>>>
>>>> void main()
>>>> {
>>>> foo!(int,int,bar)(1,2);
>>>> foo(1,2);
>>>> }
>>>
>>> Cool. Didn't know you can do that, but I guess it makes sense that it
>>> would work that way.
>>>
>>> The only thing I wish for is if I didn't have to explicitly define what
>>> T and T2 were and I could just do
>>>
>>> foo!(bar)(1,2);
>>
>> The following works and is news to me. Apparently template parameters
>> with default values need not be at the end of the template parameter
>> list:
>>
>> void foo(alias thing = (){}, T, T2)(T a, T2 b)
>> {
>> thing();
>> }
>>
>> void bar(){}
>>
>> void main()
>> {
>> foo!(bar)(1,2);
>> }
>>
>> Ali
>
> 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?
February 26, 2012
Re: Make alias parameter optional?
On Saturday, 25 February 2012 at 23:10:51 UTC, Ary Manzana wrote:
> On 2/25/12 7:31 PM, Robert Rouse wrote:
>>
>>
>> On Saturday, 25 February 2012 at 22:12:55 UTC, Ali Çehreli 
>> wrote:
>>> On 02/25/2012 01:55 PM, Robert Rouse wrote:
>>>> On Saturday, 25 February 2012 at 18:54:35 UTC, Trass3r wrote:
>>>>> void foo(T, T2, alias thing = (){})(T a, T2 b)
>>>>> {
>>>>> thing();
>>>>> }
>>>>>
>>>>> void bar(){}
>>>>>
>>>>> void main()
>>>>> {
>>>>> foo!(int,int,bar)(1,2);
>>>>> foo(1,2);
>>>>> }
>>>>
>>>> Cool. Didn't know you can do that, but I guess it makes 
>>>> sense that it
>>>> would work that way.
>>>>
>>>> The only thing I wish for is if I didn't have to explicitly 
>>>> define what
>>>> T and T2 were and I could just do
>>>>
>>>> foo!(bar)(1,2);
>>>
>>> The following works and is news to me. Apparently template 
>>> parameters
>>> with default values need not be at the end of the template 
>>> parameter
>>> list:
>>>
>>> void foo(alias thing = (){}, T, T2)(T a, T2 b)
>>> {
>>> thing();
>>> }
>>>
>>> void bar(){}
>>>
>>> void main()
>>> {
>>> foo!(bar)(1,2);
>>> }
>>>
>>> Ali
>>
>> 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.
February 26, 2012
Re: Make alias parameter optional?
On 2/25/12, Ali Çehreli <acehreli@yahoo.com> wrote:
> Apparently template parameters
> with default values need not be at the end of the template parameter list

Well it would make variadic templates rather hard to use if this was illegal:

void print(bool pretty = false, T...)(T args) { }
void main() { print(1, "two", 3.0); }
February 26, 2012
Re: Make alias parameter optional?
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
February 26, 2012
Re: Make alias parameter optional?
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");
    };
}

-- 
/Jacob Carlborg
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home