Template alias parameter does not accept types

Steven Schveighoffer schveiguy at yahoo.com
Thu May 9 06:54:25 PDT 2013


On Thu, 09 May 2013 09:38:29 -0400, Dicebot <m.strashun at gmail.com> wrote:

> On Thursday, 9 May 2013 at 13:27:35 UTC, Steven Schveighoffer wrote:
>> template GetString(T) if (is(T == int) || is(T == float) || ...)
>> {
>>    enum string GetString = T.stringof;
>> }
>
> Current idiomatic D way to handle both built-in types and symbols looks  
> like this:
>
> template Hello(T...)
>      if (T.length == 1)
> {
>      static if (is(T[0]))
>          // ...
>      else
>          // ...
> }
>
> I actually don't understand why it accepts literals as those are not  
> symbols and, for example, you can't attach UDA to literal.

I think it was a happy accident, or else a relaxation of the rules to  
allow more flexibility.  Possibly the same reason could be used to relax  
the rules again.  But I think it would be a more difficult prospect in the  
compiler.

alias cannot accept literals in a normal alias statement.  But what  
allowing it for template alias parameters does is enable the whole  
std.algorithm acceptance of a string literal for function lambdas.   
Arguably, this is no longer needed due to the new lambda syntax (but I  
think we have found other uses for it).  It has also caused some headaches:

map!"a.x"(foo)

is a different instantiation with identical code to:

map!" a.x"(foo)

-Steve


More information about the Digitalmars-d-learn mailing list