Minor feature request

ZombineDev via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 28 08:39:58 PDT 2016


On Tuesday, 28 June 2016 at 15:03:01 UTC, Steven Schveighoffer 
wrote:
> On 6/28/16 10:07 AM, Ola Fosheim Grøstad wrote:
>> On Tuesday, 28 June 2016 at 13:50:42 UTC, Steven Schveighoffer 
>> wrote:
>>> On 6/28/16 7:35 AM, Ola Fosheim Grøstad wrote:
>>>>
>>>> alias func = (int i) => i*i;
>>>>
>>>> ?
>>>>
>>>
>>> Is that valid in the compiler, or are you proposing it? I 
>>> haven't used
>>> or seen such a thing.
>>
>> It does work:
>> ----
>> import std.stdio;
>>
>> alias func1 = (int i) => i*i;
>> alias func2 = function int (int i){ return i+i;};
>>
>> void main(){
>>     writeln(func1(3), " ", func2(4), " ", func1);
>> }
>> ----
>>
>> https://dpaste.dzfl.pl/5d35ab068c2b
>
> That's pretty cool. Unfortunately, it's still a 
> delegate/function.
>
> I was thinking to define actual functions this way.
>
> -Steve

It was added with DMD 2.070 
(http://dlang.org/changelog/2.070.0.html#alias-funclit) and helps 
unify normal aliases with alias template parameters.
However similar to member delegates, it does not have access to 
the enclosing aggregate's members.

struct S
{
     int x;

     // Doesn't work:
     auto inc = () => x++;

     // Neither does this:
     alias add = (amount) => x += x = cast(int)(x + amount);

     void memberFun() // But this is ok:
     {
         alias add = (amount) => x = cast(int)(x + amount);

         add('1');
         add(2UL);
         add(3.5f);
         add(4.0);
     }
}


More information about the Digitalmars-d mailing list