I dun a DIP, possibly the best DIP ever

Steven Schveighoffer schveiguy at gmail.com
Fri Apr 24 12:47:05 UTC 2020


On 4/24/20 12:15 AM, Walter Bright wrote:
> On 4/22/2020 5:04 AM, Manu wrote:
>> [...]
> 
> Ok, I've had a chance to think about it. It's a scathingly brilliant idea!
> 
> But (there's always a but!) something stuck out at me. Consider arrays:
> 
> 
> void test()
> {
>      auto a = [1, 2, 3];
>      int[3] b = a[]*a[]; // b[0] = a[0]*a[0]; b[1] = a[1]*a[1]; b[2] = 
> a[2]*a[2];
>      int[3] c = a[]*2; // c[0] = a[0]*2; c[1] = a[1]*2; c[2] = a[2]*2;
> }
> 
> These look familiar! D tuples already use array syntax - they can be 
> indexed and sliced. Instead of the ... syntax, just use array syntax!

Hm... but how do you know where to expand the expression and where to stop?

With Manu's proposal:

foo(bar(T)...) -> foo(bar(T[0]), bar(T[1]))

foo(bar(T))... -> foo(bar(T[0])), foo(bar(T[1]))

With array proposal:

foo(bar(T)) -> which one?

> 
> The examples from the DIP:
> 
> =====================================
> --- DIP
> (Tup*10)...  -->  ( Tup[0]*10, Tup[1]*10, ... , Tup[$-1]*10 )
> 
> --- Array syntax
> Tup*10
> 
> ====================================
> --- DIP
> alias Tup = AliasSeq!(1, 2, 3);
> int[] myArr;
> assert([ myArr[Tup + 1]... ] == [ myArr[Tup[0] + 1], myArr[Tup[1] + 1], 
> myArr[Tup[2] + 1] ]);
> 
> --- Array
> alias Tup = AliasSeq!(1, 2, 3);
> int[] myArr;
> assert([ myArr[Tup + 1] ] == [ myArr[Tup[0] + 1], myArr[Tup[1] + 1], 
> myArr[Tup[2] + 1] ]);

Why is it not

assert( [myArr[Tup + 1] ] == [ myArr[Tup[0] + 1]], myArr[Tup[1] + 1]]

I think you absolutely need the tag to say which expressions are 
affected. An important thing to note is that we can't store expressions 
from statement to statement. This doesn't work (assuming array proposal):

alias arrayParams = myArr[Tup + 1]; // error, can't read blah blah blah 
at compile time.

auto newArr = [arrayParams]; // would be the equivalent of [ myArr[Tup + 
1]... ]

Another big problem is that tuples already bind to parameter lists, so 
this would be a breaking change or else be crippled for many uses.

e.g.:

foo(int x, int y = 5);

alias seq = AliasSeq!(1, 2);

foo(seq) -> foo(1, 5), foo(2, 5) or foo(1, 2)?

-Steve


More information about the Digitalmars-d mailing list