An idea to improve eponymous templates

Timon Gehr timon.gehr at gmx.ch
Thu Apr 12 12:03:25 PDT 2012


On 04/12/2012 08:19 PM, Steven Schveighoffer wrote:
> On Thu, 12 Apr 2012 14:04:44 -0400, Timon Gehr <timon.gehr at gmx.ch> wrote:
>
>> The general idea is useful, but there are issues.
>>
>> 'this' is the wrong keyword for the job. A class or struct instance is
>> very different from a template instance and the proposed usage would
>> clash with existing and useful ones. (assuming that it would work for
>> symbol lookups in addition to declarations)
>> 'template' or maybe 'scope' would be superior choices.
>
> I think static is underused keyword, maybe it should have a dual meaning
> here.
>
> In all seriousness, I'm not sure template is right either.

It is better than 'this', because it is unambiguous. I didn't say it was 
right. ;)

> Scope sounds like a very bad choice to me.
>

Why? Templates are parameterized scopes.

> Are there any eponymous examples where the name of the template is the
> first token?

No, there isn't even a declaration that starts with the identifier it 
defines.

> If so that would clash with declaring a new template.
>
> I would find this weird:
>
> template X(T)
> {
>     alias T template;
>     template t {
>        ...
>     }
> }
> symbol
> It looks like you are using template as an alias to T, but the second
> 'template' inside is using it for creating a new template.
>
> Also, consider this:
>
> template X(T)
> {
>     class template {
>        static typeof(this) create() { return new typeof(this);}
>     }
> }
>
> I don't think you could realistically use any keyword instead of
> typeof(this), I really think the best solution is what we have,

+1.

> or use a new keyword.
>

We certainly don't have enough of those!

> Another option is to do something like:
>
> @eponymous template X(T) // changed from Y(T)
> {
>     class Y { } // oops forgot to change this to X!
> }
>
> Error: template X did not declare eponymous symbol
>
> At least changes would be loud errors.
>
> -Steve

Well, there is always the possibility of just doing it like this:

template X(T) {
     enum X = result; // single line in template body that needs to 
change if there is a name change
     static if(is(T==int)) enum result = foo;
     else static if(is(T==real)) enum result = bar;
     else static if(is(T==double)) enum result = baz;
     else enum result = qux;
}

Unless someone can come up with a super-elegant way to resolve all the 
issues raised, I don't actually think that the proposal adds enough value.


More information about the Digitalmars-d mailing list