DIP63 : operator overloading for raw templates
Timon Gehr via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jun 15 15:49:31 PDT 2014
On 06/15/2014 08:32 PM, Dicebot wrote:
> http://wiki.dlang.org/DIP63
>
> This is solution for a problem I am currently having with implementing
> http://wiki.dlang.org/DIP54 (afair it was also mentioned by Timon Gehr
> during old discussion of that DIP)
>
> New proposed semantics ( to catch your attention and get to read the
> link ;) ):
>
> template Pack(T...)
> {
> alias expand = T;
>
> alias opIndex(size_t index) = T[index];
> alias opSlice(size_t lower, size_t upper) = Pack!(T[lower..upper]);
> alias opDollar = T.length;
> }
>
> // no ambiguity as Pack!(int, int) is not a valid type
> // is(element == int)
> alias element = Pack!(int, int)[1];
LGTM. Maybe you can add something along the following lines as another
motivating use case:
struct Tuple(T...){
T expand;
template Pack(){
auto opSlice(size_t lower, size_t upper){
return tuple(expand[lower..upper]);
}
}
alias Pack!() this;
}
auto tuple(T...)(T args){ return Tuple!T(args); }
void main(){
Tuple!(double,int,string) t1=tuple(1.0,2,"three");
auto t2=t1[1..$];
static assert(is(typeof(t2)==Tuple!(int,string)));
foreach(i,v;t1) writeln(i,": ",v);
}
I.e. this solution is general enough to fix the "unhygienic" behaviour
of Phobos tuple slicing.
More information about the Digitalmars-d
mailing list