dropping parentheses on template instantiation

Jarrett Billingsley jarrett.billingsley at gmail.com
Sun Oct 5 13:41:19 PDT 2008


On Sun, Oct 5, 2008 at 4:25 PM, Ary Borenszweig <ary at esperanto.org.ar> wrote:
> Jarrett Billingsley escribió:
>>
>> On Sun, Oct 5, 2008 at 2:18 PM, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>>
>>> I just realized something different. After making an informal review of
>>> some
>>> code, I saw that a large percentage of template instantiations only need
>>> ONE
>>> argument.
>>>
>>> This makes me think, with the old "!" notation, parentheses could be
>>> dropped
>>> entirely without prejudice:
>>>
>>> auto covariance = Matrix!real(n, n);
>>> auto normalized = SparseVector!double(n);
>>>
>>> and so on.
>>>
>>> To the unbridled joy of the enemies of the Sad Pirate, the dot won't work
>>> for template instantiation because without the parentheses it DOES
>>> engender
>>> ambiguity.
>>>
>>> Now say we take the following route:
>>>
>>> 1) We find something different from shouting
>>>
>>> 2) We drop the parentheses for 1 argument
>>>
>>> That sounds like a possible winner. In this case the "#" becomes
>>> considerably more attractive, in fact very attractive exactly because it
>>> looks unlike any letter:
>>>
>>> auto covariance = Matrix#real(n, n);
>>> auto normalized = SparseVector#double(n);
>>>
>>> Ideas?
>>>
>>>
>>> Andrei
>>>
>>> P.S. The Sad Pirate is the emoticon
>>>
>>> .(
>>>
>>> It doesn't have an eye and is sad, too.
>>>
>>
>> Hmm.
>>
>> I'm trying to think -- is there anywhere in the grammar where something
>> like
>>
>> Matrix int
>>
>> would be legal as something else?
>>
>> That is, I'm suggesting that we take a page from i.e. Haskell's book,
>> and simply allow the first argument to a template to be separated from
>> the template name with a space.
>>
>> auto covariance = Matrix real(n, n);
>> auto normalized = SparseVector double(n);
>>
>> Positive real x = 3.4;
>> Positive real y = 6.8;
>> Positive real z = y - x; // error
>>
>> "Identifier Identifier" does not occur anywhere in the grammar either,
>> as far as I can tell.
>>
>> Matrix Positive real(n, n);
>>
>> Please comment!(int);
>
> Identifier Identifier --> Type name
>
> it's used for declarations. Then if the parser sees:
>
> Type name
>
> , that could be a template instantiation, or maybe a variable declaration.
> It all depends on what follows next. But I think it just can be ; or =...
> I'm not sure.
>

Ugh, that's a very obvious case I've missed :P

It's worse if you consider something like:

template Templ(T)
{
    T Templ;
}

alias int foo;

Templ foo = 5;

This would parse as a variable declaration using Templ as the type and
foo as the name, but it could also mean "Templ!(foo) = 5" which
assigns 5 into the static variable held in Templ.

Scratch that!



More information about the Digitalmars-d mailing list