Using std.conv.to with enum that is based on string type

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Sat Jul 19 08:27:27 PDT 2014


On 07/19/2014 04:53 AM, Uranuz wrote:

 > When I tried to use std.conv.to to serialize enum values that are based
 > on string type I faced with situation that *to* not exactly as I
 > expected.

Maybe I misunderstand you but the behavior is consistent for all enum 
types in both conversion directions. The value 1 does not appear 
anywhere in the following conversions, so "hello" should not appear either:

import std.conv;

enum I : int    { a = 1 }
enum S : string { a = "hello" }

void main()
{
     assert(I.a.to!string == "a");
     assert(S.a.to!string == "a");

     assert("a".to!I == I.a);
     assert("a".to!S == S.a);
}

Doing anything special for a particular type would be confusing and make 
at least template programming hard.

Ali



More information about the Digitalmars-d mailing list