Make alias parameter optional?
Robert Rouse
robert.e.rouse at gmail.com
Mon Feb 27 11:34:43 PST 2012
On Monday, 27 February 2012 at 14:57:56 UTC, Ary Manzana wrote:
> On 2/25/12 10: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:
>>>>
>>>>
>>>> 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.
>>
>
> A Ruby block is much more than a delegate, because you can
> assign variables outside of the block and even returning from a
> block returns from the function that invokes it. I don't see
> how D can accomplish that.
>
> Here's some more about it:
> http://yehudakatz.com/2012/01/10/javascript-needs-blocks/
Which thing? Delegates can already access variables outside,
because that's what they are for.
More information about the Digitalmars-d-learn
mailing list