No we should not support enum types derived from strings

Paul Backus snarwin at gmail.com
Fri May 7 17:02:17 UTC 2021


On Friday, 7 May 2021 at 16:30:26 UTC, Adam D. Ruppe wrote:
>
> For better or for worse, D templates do not participate in 
> conversion and we shouldn't pretend that they do. This is often 
> times very useful - you don't want to lose information in many 
> templates. But there's other times when that information 
> doesn't matter and it would be nice it you didn't have to think 
> about it....

We can already *almost* express this in the language. This code 
works:

     void fun(T : string, T val)() {
         pragma(msg, "instantiated with ", T.stringof);
     }

     enum E : string { x = "hello" }

     alias test = fun!(E, E.x);
     // prints: instantiated with E

But if you try to write it the more natural way, with the value 
parameter first, and have the compiler deduce the type, you get 
an error:

     void fun(T val, T : string)() {
         pragma(msg, "instantiated with ", T.stringof);
     }
     // Error: undefined identifier `T`


More information about the Digitalmars-d mailing list