DIP63 : operator overloading for raw templates

matovitch via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 16 05:37:54 PDT 2014


On Monday, 16 June 2014 at 08:56:24 UTC, Marc Schütz wrote:
> On Sunday, 15 June 2014 at 18:32:31 UTC, 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];
>
> I guess there's no way around it, but it seems inconsistent 
> that the operators are defined as templated aliases instead of 
> functions as anywhere else. Could you add a paragraph to the 
> DIP which points this out and give a short justification?

+1

And why these operators only ?

struct StaticVariant
{
     alias opAssign(T t) = (immutable T x = t; alias x this;)
}

struct Json
{
     immutable (StaticVariant[string]);
     alias opDispatch(string s, T t) = (data[s] ~= t);
     alias opDispatch(string s) = data[s];
}

void main()
{

    Json john;

    john.size = 1.78;
    john.name = "John";

    float johnSize = john.size;
    string johnName = john.name;
}

Ok I am a dreaming D beginner. In this case multiple alias this 
would be better. Also, I don't understand why we can't append 
immutable array thanks to CTFE too. D is awesome in lots of ways 
but it's quite frustrating too because it frees your imagination. 
Where do we set the limit in terms of meta-meta-stuff ?



More information about the Digitalmars-d mailing list