D-Programming-Language/phobos — pull request #897 by AndrejMitrovic (2012-10-26 23:54:47+00:00)
Title: Fix Issue 8143 - Safe std.conv.to enum conversion

http://d.puremagic.com/issues/show_bug.cgi?id=8143

This tries to convert enum base values into an enum safely. If no member values matches, or if more than one member match the value it throws a ConvException.

It doesn't support floating-point base types because in most cases people won't get expected results due to floating-point semantics w.r.t. comparisons (I have proposed to add a comparator function pointer as an optional parameter, but I'll leave that up to discussion).

It doesn't support string base types because we already have a `toImpl` overload which converts strings to enums by doing comparison against enum member **names**, rather than their **values**.

Unfortunately this makes this pull problematic. If people start using `to!()` expecting member value comparisons they might think that string comparisons work the same way. IOW:

```d
enum EnInt : int { A = 10 }
EnInt a = to!EnInt(10); // ok with this pull

enum EnStr : string { A = "abc" }
EnStr s = to!EnStr("abc"); // fails, because a different toImpl is used which compares member names
```

Personally I'm beginning to think that using `to` for converting enum member names from string to enum might have been a bad idea, but it's way too late to change things now.

I've started a discussion a few days ago but haven't gotten any replies yet:
http://forum.dlang.org/thread/mailman.1232.1351123009.5162.digitalmars-d@puremagic.com

As a last resort we could consider introducing a new name for the function in this pull.