digitalmars.D.learn - cannot alias array ;/
- Jot (6/6) Jan 18 2017 alias a = myarray[k];
- =?UTF-8?Q?Ali_=c3=87ehreli?= (20/26) Jan 19 2017 Nested functions work pretty well in some cases:
- ketmar (14/20) Jan 19 2017 alias is not a macro, it is alias to *symbol*. only symbol, not
- Dukc (18/20) Jan 21 2017 In fact, it can nowadays be. You just have to mark it so, with a
- John Colvin (10/16) Jan 19 2017 Simplest solution, has brackets that you may not like:
- Jot (2/21) Jan 19 2017 I just use a pointer and dereference ;/
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
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
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
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
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
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:I just use a pointer and dereference ;/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