www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cannot alias array ;/

reply Jot <Jot datacentralave.com> writes:
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.
Jan 18 2017
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jan 19 2017
prev sibling next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
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]); } }
Jan 19 2017
parent Dukc <ajieskola gmail.com> writes:
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.
Jan 21 2017
prev sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
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]]); }
Jan 19 2017
parent Jot <Jot datacentralave.com> writes:
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 ;/
Jan 19 2017