cannot alias array ;/

Dukc via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 21 00:21:31 PST 2017


On Thursday, 19 January 2017 at 08:06:04 UTC, ketmar wrote:
> alias is not a macro, it is alias to *symbol*. only symbol, not 
> any arbitrary expression.

In fact, it can nowadays be. You just have to mark it so, with a 
lambda:

void main()
{   import std.stdio;
     auto myArray = [2, 3, 5, 6];
     int k = 2;
     alias a = () => myArray[k];
     writeln(a());
     k = 3;
     writeln(a());
}

//result:
//5
//6

The downside with this (and function pointers and delegates) 
compared to defining functions is that you cannot call it without 
parenthesis.




More information about the Digitalmars-d-learn mailing list