Thread overview
cannot alias array ;/
Jan 19, 2017
Jot
Jan 19, 2017
Ali Çehreli
Jan 19, 2017
ketmar
Jan 21, 2017
Dukc
Jan 19, 2017
John Colvin
Jan 20, 2017
Jot
January 19, 2017
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.

January 19, 2017
On 01/18/2017 11:48 PM, 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.
>

Nested functions work pretty well in some cases:

import std.stdio;

void foo() {
    int[double][char][string] aa;
    aa["hello"]['b'][ 3.5] = 35;

    auto a = "hello";
    char b = 'b';

    // Works like an alias:
    ref theOne() {
        return aa[a][b];
    }

    theOne[1.5] = 15;
    theOne[2.5] = 25;

    writeln(aa);
}

void main() {
    foo();
}

Ali

January 19, 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]);
  }
}
January 19, 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.

Simplest solution, has brackets that you may not like:

void main()
{
	int[][] a = [[1]];
	size_t k = 0;
	ref foo() { return a[k]; };
	foo() = [3];
	assert(a == [[3]]);
}
January 20, 2017
On Thursday, 19 January 2017 at 12:50:06 UTC, John Colvin wrote:
> 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.
>
> Simplest solution, has brackets that you may not like:
>
> void main()
> {
> 	int[][] a = [[1]];
> 	size_t k = 0;
> 	ref foo() { return a[k]; };
> 	foo() = [3];
> 	assert(a == [[3]]);
> }

I just use a pointer and dereference ;/
January 21, 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.