Re: Getting enum from value

2017-08-06 Thread Kreikey via Digitalmars-d-learn
On Saturday, 5 August 2017 at 20:11:27 UTC, Matthew Remmel wrote: On Saturday, 5 August 2017 at 18:26:10 UTC, Kreikey wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum tha

Re: Getting enum from value

2017-08-05 Thread Kreikey via Digitalmars-d-learn
On Saturday, 5 August 2017 at 20:11:27 UTC, Matthew Remmel wrote: On Saturday, 5 August 2017 at 18:26:10 UTC, Kreikey wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel I'm annoyed that I didn't think of trying to cast it. That works great if the value exists in the enum. It do

Re: Getting enum from value

2017-08-05 Thread Matthew Remmel via Digitalmars-d-learn
On Saturday, 5 August 2017 at 18:26:10 UTC, Kreikey wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e [...] Capitals c =

Re: Getting enum from value

2017-08-05 Thread Kreikey via Digitalmars-d-learn
On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e [...] Capitals c = cast(Capitals)"Chicago"; writeln(c);// Illinois

Re: Getting enum from value

2017-08-05 Thread ag0aep6g via Digitalmars-d-learn
On 08/05/2017 07:05 PM, ag0aep6g wrote: E enumFromValue(E)(string s) The type of `s` should probably be a template parameter as well.

Re: Getting enum from value

2017-08-05 Thread ag0aep6g via Digitalmars-d-learn
On 08/05/2017 05:33 PM, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e enum Capitals { Indiana = "Indianapolis", Illinois = "Chicago", Ohio = "Columbus" } Ca

Re: Getting enum from value

2017-08-05 Thread Matthew Remmel via Digitalmars-d-learn
On Saturday, 5 August 2017 at 15:42:53 UTC, Rene Zwanenburg wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: Any ideas? You can use to! in std.conv: import std.stdio; import std.conv; enum Foo { A = "A", B = "B" } void main() { writeln("A".to

Re: Getting enum from value

2017-08-05 Thread Temtaime via Digitalmars-d-learn
On Saturday, 5 August 2017 at 15:42:53 UTC, Rene Zwanenburg wrote: On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: Any ideas? You can use to! in std.conv: import std.stdio; import std.conv; enum Foo { A = "A", B = "B" } void main() { writeln("A".to

Re: Getting enum from value

2017-08-05 Thread Rene Zwanenburg via Digitalmars-d-learn
On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: Any ideas? You can use to! in std.conv: import std.stdio; import std.conv; enum Foo { A = "A", B = "B" } void main() { writeln("A".to!Foo); }

Re: Getting enum from value

2017-08-05 Thread Stefan Koch via Digitalmars-d-learn
On Saturday, 5 August 2017 at 15:33:57 UTC, Matthew Remmel wrote: I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e [...] What you want is already in the standard library. std.conv.to can convert

Getting enum from value

2017-08-05 Thread Matthew Remmel via Digitalmars-d-learn
I feel like I'm missing something, but there has to be an easier way to convert a value into an enum than switching over every possible value: i.e enum Capitals { Indiana = "Indianapolis", Illinois = "Chicago", Ohio = "Columbus" } Capitals enumFromValue(string s) { switch (s) {