digitalmars.D.learn - Runtime evaluation
- nrgyzer (25/25) Jul 07 2011 Hi guys,
- David Nadlinger (5/30) Jul 07 2011 You could generate the if/else-chain, lookup table, etc. via
- Mafi (4/29) Jul 08 2011 Metaprogramming is the keyword but you don't have to do it yourself.
- nrgyzer (5/42) Jul 09 2011 do this or do I've to use another method (like if/else or switch/
Hi guys,
I'm trying to read a string from a text file which contains a value of an
enumeration like:
enum MyEnum : string {
Entry_1 = "abc",
Entry_2 = "def",
Entry_3 = "ghi",
}
Stream s = new File("myFile.ext", FileMode.In);
uint len;
s.read(len);
string entry = cast(string) s.readString(len);
s.close();
writeln(mixin("MyEnum." ~ entry));
myFile.ext may contain:
...
Entry_2
Entry_1
Entry_2
Entry_3
...
But mixin's are for compile time only... is there any chance to do this or do
I've to use another method (like if/else or switch/case):
if (entry == "Entry_1") ...
else if (entry == "Entry_2") ...
...
Thanks a lot!
Jul 07 2011
You could generate the if/else-chain, lookup table, etc. via
metaprogramming to save you the typing and code duplication (probably a
foreach over std.traits.EnumMembers will be involved).
David
On 7/7/11 11:23 PM, nrgyzer wrote:
Hi guys,
I'm trying to read a string from a text file which contains a value of an
enumeration like:
enum MyEnum : string {
Entry_1 = "abc",
Entry_2 = "def",
Entry_3 = "ghi",
}
Stream s = new File("myFile.ext", FileMode.In);
uint len;
s.read(len);
string entry = cast(string) s.readString(len);
s.close();
writeln(mixin("MyEnum." ~ entry));
myFile.ext may contain:
...
Entry_2
Entry_1
Entry_2
Entry_3
...
But mixin's are for compile time only... is there any chance to do this or do
I've to use another method (like if/else or switch/case):
if (entry == "Entry_1") ...
else if (entry == "Entry_2") ...
...
Thanks a lot!
Jul 07 2011
Am 07.07.2011 23:23, schrieb nrgyzer:
Hi guys,
I'm trying to read a string from a text file which contains a value of an
enumeration like:
enum MyEnum : string {
Entry_1 = "abc",
Entry_2 = "def",
Entry_3 = "ghi",
}
Stream s = new File("myFile.ext", FileMode.In);
uint len;
s.read(len);
string entry = cast(string) s.readString(len);
s.close();
writeln(mixin("MyEnum." ~ entry));
myFile.ext may contain:
...
Entry_2
Entry_1
Entry_2
Entry_3
...
But mixin's are for compile time only... is there any chance to do this or do
I've to use another method (like if/else or switch/case):
if (entry == "Entry_1") ...
else if (entry == "Entry_2") ...
...
Thanks a lot!
Metaprogramming is the keyword but you don't have to do it yourself.
Just use std.conv.to!MyEnum(string_without_the prefix).
Mafi
Jul 08 2011
== Auszug aus Mafi (mafi example.org)'s ArtikelAm 07.07.2011 23:23, schrieb nrgyzer:value of an enumeration like:Hi guys, I'm trying to read a string from a text file which contains ado this or do I've to use another method (like if/else or switch/ case):enum MyEnum : string { Entry_1 = "abc", Entry_2 = "def", Entry_3 = "ghi", } Stream s = new File("myFile.ext", FileMode.In); uint len; s.read(len); string entry = cast(string) s.readString(len); s.close(); writeln(mixin("MyEnum." ~ entry)); myFile.ext may contain: ... Entry_2 Entry_1 Entry_2 Entry_3 ... But mixin's are for compile time only... is there any chance toThanks Mafi, exactly what I'm looking for :)if (entry == "Entry_1") ... else if (entry == "Entry_2") ... ... Thanks a lot!Metaprogramming is the keyword but you don't have to do it yourself. Just use std.conv.to!MyEnum(string_without_the prefix). Mafi
Jul 09 2011









David Nadlinger <see klickverbot.at> 