[Issue 5515] std.conv.to for safer enum casts
https://issues.dlang.org/show_bug.cgi?id=5515 Dmitry Olshanskychanged: What|Removed |Added Status|REOPENED|RESOLVED CC||dmitry.o...@gmail.com Resolution|--- |FIXED --- Comment #6 from Dmitry Olshansky --- import std.conv: to; import std.algorithm: map; enum Foo : char { A='a', B='b', C='c' } void main() { auto foos = "abcabcabc".map!(to!Foo)(); } Compiles today, that was the last of it. --
[Issue 5515] std.conv.to for safer enum casts
https://issues.dlang.org/show_bug.cgi?id=5515 Andrei Alexandrescuchanged: What|Removed |Added Keywords||bootcamp CC||and...@erdani.com --
[Issue 5515] std.conv.to for safer enum casts
http://d.puremagic.com/issues/show_bug.cgi?id=5515 Andrej Mitrovic andrej.mitrov...@gmail.com changed: What|Removed |Added Status|NEW |RESOLVED CC||andrej.mitrov...@gmail.com Resolution||DUPLICATE --- Comment #2 from Andrej Mitrovic andrej.mitrov...@gmail.com 2013-02-17 15:05:24 PST --- Implemented in Issue8143. *** This issue has been marked as a duplicate of issue 8143 *** -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 5515] std.conv.to for safer enum casts
http://d.puremagic.com/issues/show_bug.cgi?id=5515 --- Comment #3 from bearophile_h...@eml.cc 2013-02-17 16:04:00 PST --- (In reply to comment #2) Implemented in Issue8143. *** This issue has been marked as a duplicate of issue 8143 *** Given that Enums with floating-point or string base types are not supported. this is more a WONTFIX :-) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 5515] std.conv.to for safer enum casts
http://d.puremagic.com/issues/show_bug.cgi?id=5515 --- Comment #4 from bearophile_h...@eml.cc 2013-02-17 16:33:03 PST --- But probably this should be supported: import std.conv: to; enum Foo : char { A = 'a' } void main() { dchar d = 'a'; Foo f = to!Foo(d); } Currently it gives: ...\dmd2\src\phobos\std\conv.d(274): Error: template std.conv.toImpl does not match any function template declaration. Candidates are: ... It's useful when you want to write (the argument of this map is a dchar): import std.conv: to; import std.algorithm: map; enum Foo : char { A='a', B='b', C='c' } void main() { auto foos = abcabcabc.map!(to!Foo)(); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 5515] std.conv.to for safer enum casts
http://d.puremagic.com/issues/show_bug.cgi?id=5515 Andrej Mitrovic andrej.mitrov...@gmail.com changed: What|Removed |Added Status|RESOLVED|REOPENED Resolution|DUPLICATE | --- Comment #5 from Andrej Mitrovic andrej.mitrov...@gmail.com 2013-02-17 16:46:53 PST --- (In reply to comment #4) But probably this should be supported: import std.conv: to; enum Foo : char { A = 'a' } void main() { dchar d = 'a'; Foo f = to!Foo(d); } Ok. Reopening issue. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---
[Issue 5515] std.conv.to for safer enum casts
http://d.puremagic.com/issues/show_bug.cgi?id=5515 --- Comment #1 from bearophile_h...@eml.cc 2011-08-18 14:51:07 PDT --- This enhancement request comes from a handy feature of the Ada language. It's not too much hard to implement something similar in D too, with I think an acceptable final user syntax: import std.traits: EnumMembers, OriginalType, Unqual; import std.stdio: writeln; private E[] convertEnum(E, T)(in T[] data) @safe pure nothrow if (is(E == enum) is(Unqual!T == OriginalType!Foo)) { //assert(__ctfe, This is a compile-time function only.); E[T] dict; foreach (member; EnumMembers!E) dict[member] = member; auto result = new E[data.length]; foreach (i, item; data) result[i] = dict[item]; return result; } enum Foo : char { A='a', B='b', C='c' } void show(T)(T x) { writeln(x); } template F(string s) { enum F = convertEnum!Foo(s); } void main() { enum Foo[][] foos = [F!abcabcabc, F!cbacbacba]; //import std.conv; //const Foo[] foos2 = to!(Foo[])(abcabcabc); // not possible yet show(foos); // [[A, B, C, A, B, C, A, B, C], // [C, B, A, C, B, A, C, B, A]] } Still, I think safe compile-time Enum conversion and safe run-time Enum conversion is a feature worth folding inside to!(). -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email --- You are receiving this mail because: ---