Proposed D2 Feature: => for anonymous delegates

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Oct 21 06:48:32 PDT 2009


Don wrote:
> Andrei Alexandrescu wrote:
>> language_fan wrote:
>>> Wed, 21 Oct 2009 12:35:41 +0200, Pelle Månsson thusly wrote:
>>>
>>>> Andrei Alexandrescu wrote:
>>>>> Pelle Månsson wrote:
>>>>>> Jason House wrote:
>>>>>>> Andrei Alexandrescu Wrote:
>>>>>>>
>>>>>>>> Jason House wrote:
>>>>>>>>> Am I the only one that has trouble remembering how to write an
>>>>>>>>> inline anonymous delegate when calling a function? At a minimum,
>>>>>>>>> both Scala and C# use (args) => { body; } syntax. Can we please
>>>>>>>>> sneak it into D2?
>>>>>>>> We have (args) { body; }
>>>>>>>>
>>>>>>>> Andrei
>>>>>>> Somehow, I missed that. What kind of type inference, if any, is
>>>>>>> allowed? Scala and C# allow omiting the type. Lately I'm doing a lot
>>>>>>> of (x) => { return x.foo(7); } in C# and it's nice to omit the
>>>>>>> amazingly long type for x. The IDE even knows the type of x for
>>>>>>> intellisense... I think scala would allow x => foo(7), or maybe even
>>>>>>> => _.foo(7) or even _.foo(7). I haven't written much scala, so I may
>>>>>>> be way off...
>>>>>> Recent experiments by myself indicate you cannot omit the type and 
>>>>>> you
>>>>>> cannot use auto for the type, so you actually need to type your
>>>>>> VeryLongClassName!(With, Templates) if you need it.
>>>>>>
>>>>>> I sort of miss automatic type deduction.
>>>>> Actually, full type deduction should be in vigor, but it is known that
>>>>> the feature has more than a few bugs. Feel free to report any instance
>>>>> in which type deduction does not work in bugzilla.
>>>>>
>>>>> Andrei
>>>> int f(int delegate(int) g) {
>>>>      return g(13);
>>>> }
>>>> void main() {
>>>>      f((auto x) { return x+13; });
>>>> }
>>>>
>>>> This does not compile in D v2.034. Am I missing something?
>>>
>>> No, in this context the exact type can be inferred unambiguously 
>>> without worrying about overloading.
>>
>> The program should be roughly equivalent with:
>>
>> int f(int delegate(int) g) {
>>     return g(13);
>> }
>>
>> auto __fun(T)(T x) { return x+13; }
>>
>> void main() {
>>     f(&__fun);
>> }
>>
>> which fails with
>>
>> Internal error: e2ir.c 644
>>
>> :o)
> 
> Can't reproduce that. It doesn't ICE for me. Tried on several DMD 
> versions. (Doesn't compile, though).

Sorry, it's the emacs paste buffer being different from Thunderbird's 
that's the problem. Please try this.

int f(int delegate(int) g) {
     return g(13);
}

void main() {
     auto __fun(T)(T x) { return x+13; }
     f(cast(int delegate(int)) __fun);
}

On my machine:

__fun(T)
Internal error: e2ir.c 644

FWIW this should work too:

int f(int delegate(int) g) {
     return g(13);
}

void main() {
     auto __fun(T)(T x) { return x+13; }
     int delegate(int) __tmp = &__fun;
     f(__tmp);
}

That doesn't compile with:

./test.d(31): Error: __fun(T) is not an lvalue
./test.d(31): Error: cannot implicitly convert expression (&(__fun(T))) 
of type void* to int delegate(int)


Andrei



More information about the Digitalmars-d mailing list