I dun a DIP, possibly the best DIP ever
Walter Bright
newshound2 at digitalmars.com
Fri Apr 24 04:15:36 UTC 2020
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!
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] ]);
===================================
---DIP
alias Values = AliasSeq!(1, 2, 3);
alias Types = AliasSeq!(int, short, float);
pragma(msg, cast(Types)Values...);
---Array
alias Values = AliasSeq!(1, 2, 3);
alias Types = AliasSeq!(int, short, float);
pragma(msg, cast(Types)Values);
=================================
---DIP
alias OnlyTwo = AliasSeq!(10, 20);
pragma(msg, (Values + OnlyTwo)...);
---Array
alias OnlyTwo = AliasSeq!(10, 20);
pragma(msg, Values + OnlyTwo);
The idea is simply if we have:
t op c
where t is a tuple and c is not, the result is:
tuple(t[0] op c, t[1] op c, ..., t[length - 1] op c)
For:
t1 op t2
the result is:
tuple(t1[0] op t2[0], t1[1] op t2[1], ..., t1[length - 1] op t2[length - 1])
The AST doesn't have to be walked to make this work, just do it as part of the
usual bottom-up semantic processing.
The advantage is:
1. no new grammar
2. no new operator precedence rules
3. turn expressions that are currently errors into doing the obvious thing
Why does C++ use ... rather than array syntax? Because C++ doesn't have arrays!
More information about the Digitalmars-d
mailing list