Using std.conv.to with enum that is based on string type
Uranuz via Digitalmars-d
digitalmars-d at puremagic.com
Sat Jul 19 04:53:45 PDT 2014
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. As far as I understand it tries to find match
between passed string value and enum value identifier instead of
just casting it to enum type.
1. So I want to know if it is expected behaviour?
2. What are the reasons for it?
There is a simple example that illustrates what I'm talking about.
import std.stdio, std.conv;
enum Category: string { first = "I", second = "II", third = "III"
};
void main()
{
assert( to!Category("first") == Category.first ); //This runs
succesfully
//assert( to!Category("I") == Category.first ); //This
conversion fails
}
As a result I could try to fix it if it's not correct behaviour.
Another concern about using *cast* is that it doesn't make some
checks if value is one of enum values declared in code.
I found some topics about enum conversions.
http://forum.dlang.org/thread/bug-4744-3@http.d.puremagic.com%2Fissues%2F
http://forum.dlang.org/thread/bug-9821-3@http.d.puremagic.com%2Fissues%2F
http://forum.dlang.org/thread/fpzhgzhrhaxjfuiydwxh@forum.dlang.org
Problem is that (as I think) that std.conv.to documentation
doesn't describe how *to* SHOULD work for different ways of
coversions so it's not obvious how to *fix* it. The only way of
using is via trials and errors and it's not garanteed that found
out with this way will not be changed and your code will not be
broken. It's because that implementers don't have information
about how exactly it SHOULD work.
Sorry for some emotions but I have problems with lack of doc
sometimes and it makes my job harder.
More information about the Digitalmars-d
mailing list