cannot alias array ;/

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 19 00:06:04 PST 2017


On Thursday, 19 January 2017 at 07:48:03 UTC, Jot wrote:
> alias a = myarray[k];
>
> fails
>
> myarray is a multidimensial array that I want to reduce writing 
> it every time but D complains that it can't alias it.
>
> I simply want it to do a direct substitution, nothing fancy, 
> just to reducing typing.

alias is not a macro, it is alias to *symbol*. only symbol, not 
any arbitrary expression.

if you want to reduce typing, consider, for example, moving your 
code to nested function and pass `myarray[k]` as ref arg to it. 
like:

void processArray (int[] myarray) {
   void doSomething (ref int a) {
      if (a == 0) a = 42; else a += 69;
   }
   foreach (immutable k; 0..myarray.length) {
     if (k%3 == 0 || k%5 == 0) doSomething(myarray[k]);
   }
}


More information about the Digitalmars-d-learn mailing list