digitalmars.D.learn - enum for beginners
- Johannes Totz (28/28) Nov 14 2011 Hi!
- Timon Gehr (14/41) Nov 14 2011 I don't know. It works for me.
- Johannes Totz (4/55) Nov 15 2011 I had 2.055 and just upgraded.
- Johannes Totz (8/71) Nov 15 2011 Ah, when I compile on the command line with:
- Steven Schveighoffer (4/77) Nov 15 2011 dmd must map to a D1 version, where string literals were char[N], not
- Steven Schveighoffer (5/19) Nov 15 2011 to check, type dmd without args on the command line, it will tell you th...
- Johannes Totz (22/41) Nov 15 2011 There is a 1.071 version somewhere in the path...
- Steven Schveighoffer (19/62) Nov 15 2011 Don't have a windows box handy, but this works on Linux:
- Johannes Totz (16/83) Nov 15 2011 -g is what makes it fail, -debug is fine.
- Mike Wey (5/33) Nov 15 2011 It's a bug: string enums don't work with -g compiler switch.
- Johannes Totz (29/71) Nov 15 2011 Ah thanks! I've added my case...
- bearophile (5/31) Nov 15 2011 The error message suggests you to use a static opCall.
- Johannes Totz (3/32) Nov 15 2011 struct works, thanks!
Hi! I'm having trouble with named typed enums. This works (unnamed): enum : string { a = "a", b = "b" } int main(string[] argv) { writeln(a); return 0; } But this does not: enum X : string { a = "a", // Error: Integer constant expression expected // instead of "a" b = "b" // Error: Integer constant expression expected // instead of "b" } int main(string[] argv) { writeln(X.a); return 0; } What did I miss? Johannes
Nov 14 2011
On 11/14/2011 11:25 PM, Johannes Totz wrote:Hi! I'm having trouble with named typed enums. This works (unnamed): enum : string { a = "a", b = "b" } int main(string[] argv) { writeln(a); return 0; } But this does not: enum X : string { a = "a", // Error: Integer constant expression expected// instead of "a" b = "b" // Error: Integer constant expression expected // instead of "b" } int main(string[] argv) { writeln(X.a); return 0; } What did I miss?I don't know. It works for me. import std.stdio; enum X : string{ a = "a", b = "b", } int main(string[] argv) { writeln(X.a); return 0; } compiles, runs and prints "a" as expected. Are you using the latest version of the compiler? (DMD v2.056)
Nov 14 2011
On 14/11/2011 22:32, Timon Gehr wrote:On 11/14/2011 11:25 PM, Johannes Totz wrote:I had 2.055 and just upgraded. But this seems to be some issue with VisualD. Compiling on the command line (with 2.056) works fine as expected.Hi! I'm having trouble with named typed enums. This works (unnamed): enum : string { a = "a", b = "b" } int main(string[] argv) { writeln(a); return 0; } But this does not: enum X : string { a = "a", // Error: Integer constant expression expected// instead of "a" b = "b" // Error: Integer constant expression expected // instead of "b" } int main(string[] argv) { writeln(X.a); return 0; } What did I miss?I don't know. It works for me. import std.stdio; enum X : string{ a = "a", b = "b", } int main(string[] argv) { writeln(X.a); return 0; } compiles, runs and prints "a" as expected. Are you using the latest version of the compiler? (DMD v2.056)
Nov 15 2011
On 15/11/2011 15:43, Johannes Totz wrote:On 14/11/2011 22:32, Timon Gehr wrote:Ah, when I compile on the command line with: dmd -g -debug main.d main.d(6): Error: enum main.X base type must be of integral type, not char[] main.d(8): Error: cannot implicitly convert expression ("a") of type char[1u] to int main.d(9): Error: cannot implicitly convert expression ("b") of type char[1u] to intOn 11/14/2011 11:25 PM, Johannes Totz wrote:I had 2.055 and just upgraded. But this seems to be some issue with VisualD. Compiling on the command line (with 2.056) works fine as expected.Hi! I'm having trouble with named typed enums. This works (unnamed): enum : string { a = "a", b = "b" } int main(string[] argv) { writeln(a); return 0; } But this does not: enum X : string { a = "a", // Error: Integer constant expression expected// instead of "a" b = "b" // Error: Integer constant expression expected // instead of "b" } int main(string[] argv) { writeln(X.a); return 0; } What did I miss?I don't know. It works for me. import std.stdio; enum X : string{ a = "a", b = "b", } int main(string[] argv) { writeln(X.a); return 0; } compiles, runs and prints "a" as expected. Are you using the latest version of the compiler? (DMD v2.056)
Nov 15 2011
On Tue, 15 Nov 2011 10:47:22 -0500, Johannes Totz <johannes jo-t.de> wrote:On 15/11/2011 15:43, Johannes Totz wrote:dmd must map to a D1 version, where string literals were char[N], not immutable(char)[]. In D1, you could not have enums that were strings. -SteveOn 14/11/2011 22:32, Timon Gehr wrote:Ah, when I compile on the command line with: dmd -g -debug main.d main.d(6): Error: enum main.X base type must be of integral type, not char[] main.d(8): Error: cannot implicitly convert expression ("a") of type char[1u] to int main.d(9): Error: cannot implicitly convert expression ("b") of type char[1u] to intOn 11/14/2011 11:25 PM, Johannes Totz wrote:I had 2.055 and just upgraded. But this seems to be some issue with VisualD. Compiling on the command line (with 2.056) works fine as expected.Hi! I'm having trouble with named typed enums. This works (unnamed): enum : string { a = "a", b = "b" } int main(string[] argv) { writeln(a); return 0; } But this does not: enum X : string { a = "a", // Error: Integer constant expression expected// instead of "a" b = "b" // Error: Integer constant expression expected // instead of "b" } int main(string[] argv) { writeln(X.a); return 0; } What did I miss?I don't know. It works for me. import std.stdio; enum X : string{ a = "a", b = "b", } int main(string[] argv) { writeln(X.a); return 0; } compiles, runs and prints "a" as expected. Are you using the latest version of the compiler? (DMD v2.056)
Nov 15 2011
On Tue, 15 Nov 2011 10:55:44 -0500, Steven Schveighoffer <schveiguy yahoo.com> wrote:On Tue, 15 Nov 2011 10:47:22 -0500, Johannes Totz <johannes jo-t.de> wrote:to check, type dmd without args on the command line, it will tell you the version. -SteveAh, when I compile on the command line with: dmd -g -debug main.d main.d(6): Error: enum main.X base type must be of integral type, not char[] main.d(8): Error: cannot implicitly convert expression ("a") of type char[1u] to int main.d(9): Error: cannot implicitly convert expression ("b") of type char[1u] to intdmd must map to a D1 version, where string literals were char[N], not immutable(char)[]. In D1, you could not have enums that were strings.
Nov 15 2011
On 15/11/2011 15:56, Steven Schveighoffer wrote:On Tue, 15 Nov 2011 10:55:44 -0500, Steven Schveighoffer <schveiguy yahoo.com> wrote:There is a 1.071 version somewhere in the path... C:\Users\...>dmd DMD32 D Compiler v1.071 But if I do... C:\Users\...>C:\D\dmd2\windows\bin\dmd.exe -g -debug main.d main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" However... C:\Users\...>C:\D\dmd2\windows\bin\dmd.exe main.d C:\Users\...>main.exe a Getting rid of 1.071... C:\Users\...>dmd DMD32 D Compiler v2.056 C:\Users\...>dmd -g -debug main.d main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b"On Tue, 15 Nov 2011 10:47:22 -0500, Johannes Totz <johannes jo-t.de> wrote:to check, type dmd without args on the command line, it will tell you the version.Ah, when I compile on the command line with: dmd -g -debug main.d main.d(6): Error: enum main.X base type must be of integral type, not char[] main.d(8): Error: cannot implicitly convert expression ("a") of type char[1u] to int main.d(9): Error: cannot implicitly convert expression ("b") of type char[1u] to intdmd must map to a D1 version, where string literals were char[N], not immutable(char)[]. In D1, you could not have enums that were strings.
Nov 15 2011
On Tue, 15 Nov 2011 11:15:01 -0500, Johannes Totz <johannes jo-t.de> wrote:On 15/11/2011 15:56, Steven Schveighoffer wrote:Don't have a windows box handy, but this works on Linux: steves steve-laptop:~/testd$ cat testenum.d import std.stdio; enum X : string { a = "a", b = "b", } void main() { writeln(X.b); } steves steve-laptop:~/testd$ ~/dmd-2.056/linux/bin32/dmd -g -debug testenum.d steves steve-laptop:~/testd$ ./testenum b Can you post your exact code that doesn't work with dmd 2.056? This is all I have from your previous post. I can't see why it wouldn't work. -SteveOn Tue, 15 Nov 2011 10:55:44 -0500, Steven Schveighoffer <schveiguy yahoo.com> wrote:There is a 1.071 version somewhere in the path... C:\Users\...>dmd DMD32 D Compiler v1.071 But if I do... C:\Users\...>C:\D\dmd2\windows\bin\dmd.exe -g -debug main.d main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" However... C:\Users\...>C:\D\dmd2\windows\bin\dmd.exe main.d C:\Users\...>main.exe a Getting rid of 1.071... C:\Users\...>dmd DMD32 D Compiler v2.056 C:\Users\...>dmd -g -debug main.d main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b"On Tue, 15 Nov 2011 10:47:22 -0500, Johannes Totz <johannes jo-t.de> wrote:to check, type dmd without args on the command line, it will tell you the version.Ah, when I compile on the command line with: dmd -g -debug main.d main.d(6): Error: enum main.X base type must be of integral type, not char[] main.d(8): Error: cannot implicitly convert expression ("a") of type char[1u] to int main.d(9): Error: cannot implicitly convert expression ("b") of type char[1u] to intdmd must map to a D1 version, where string literals were char[N], not immutable(char)[]. In D1, you could not have enums that were strings.
Nov 15 2011
On 15/11/2011 16:30, Steven Schveighoffer wrote:On Tue, 15 Nov 2011 11:15:01 -0500, Johannes Totz <johannes jo-t.de> wrote:-g is what makes it fail, -debug is fine. ---------------------------------------- module main; import std.stdio; enum X : string { a = "a", b = "b" } int main(string[] argv) { writeln(X.a); return 0; } ----------------------------------------On 15/11/2011 15:56, Steven Schveighoffer wrote:Don't have a windows box handy, but this works on Linux: steves steve-laptop:~/testd$ cat testenum.d import std.stdio; enum X : string { a = "a", b = "b", } void main() { writeln(X.b); } steves steve-laptop:~/testd$ ~/dmd-2.056/linux/bin32/dmd -g -debug testenum.d steves steve-laptop:~/testd$ ./testenum b Can you post your exact code that doesn't work with dmd 2.056? This is all I have from your previous post. I can't see why it wouldn't work.On Tue, 15 Nov 2011 10:55:44 -0500, Steven Schveighoffer <schveiguy yahoo.com> wrote:There is a 1.071 version somewhere in the path... C:\Users\...>dmd DMD32 D Compiler v1.071 But if I do... C:\Users\...>C:\D\dmd2\windows\bin\dmd.exe -g -debug main.d main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" However... C:\Users\...>C:\D\dmd2\windows\bin\dmd.exe main.d C:\Users\...>main.exe a Getting rid of 1.071... C:\Users\...>dmd DMD32 D Compiler v2.056 C:\Users\...>dmd -g -debug main.d main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b" main.d(8): Error: Integer constant expression expected instead of "a" main.d(9): Error: Integer constant expression expected instead of "b"On Tue, 15 Nov 2011 10:47:22 -0500, Johannes Totz <johannes jo-t.de> wrote:to check, type dmd without args on the command line, it will tell you the version.Ah, when I compile on the command line with: dmd -g -debug main.d main.d(6): Error: enum main.X base type must be of integral type, not char[] main.d(8): Error: cannot implicitly convert expression ("a") of type char[1u] to int main.d(9): Error: cannot implicitly convert expression ("b") of type char[1u] to intdmd must map to a D1 version, where string literals were char[N], not immutable(char)[]. In D1, you could not have enums that were strings.
Nov 15 2011
On 11/14/2011 11:25 PM, Johannes Totz wrote:Hi! I'm having trouble with named typed enums. This works (unnamed): enum : string { a = "a", b = "b" } int main(string[] argv) { writeln(a); return 0; } But this does not: enum X : string { a = "a", // Error: Integer constant expression expected // instead of "a" b = "b" // Error: Integer constant expression expected // instead of "b" } int main(string[] argv) { writeln(X.a); return 0; } What did I miss? JohannesIt's a bug: string enums don't work with -g compiler switch. http://d.puremagic.com/issues/show_bug.cgi?id=5168 -- Mike Wey
Nov 15 2011
On 15/11/2011 20:37, Mike Wey wrote:On 11/14/2011 11:25 PM, Johannes Totz wrote:Ah thanks! I've added my case... How would i go about using a custom class for the enum-type? Various combinations of the snippet below lead to errors. class EnumType { int x; alias x this; this(int i) { x = i; } void opCall(int i) { } } enum X : EnumType { a = EnumType(1), b = 2 } Errors range are always variations of main.d(16): Error: function main.EnumType.opCall need 'this' to access member opCall main.d(23): called from here: opCall(1) main.d(23): Error: cannot implicitly convert expression (opCall(1)) of type void to main.EnumType main.d(24): Error: cannot implicitly convert expression (2) of type int to main.EnumTypeHi! I'm having trouble with named typed enums. This works (unnamed): enum : string { a = "a", b = "b" } int main(string[] argv) { writeln(a); return 0; } But this does not: enum X : string { a = "a", // Error: Integer constant expression expected // instead of "a" b = "b" // Error: Integer constant expression expected // instead of "b" } int main(string[] argv) { writeln(X.a); return 0; } What did I miss? JohannesIt's a bug: string enums don't work with -g compiler switch. http://d.puremagic.com/issues/show_bug.cgi?id=5168
Nov 15 2011
Johannes Totz:class EnumType { int x; alias x this; this(int i) { x = i; } void opCall(int i) { } } enum X : EnumType { a = EnumType(1), b = 2 } Errors range are always variations of main.d(16): Error: function main.EnumType.opCall need 'this' to access member opCallThe error message suggests you to use a static opCall. And are you sure you want a class instead of a struct? Bye, bearophile
Nov 15 2011
On 15/11/2011 22:56, bearophile wrote:Johannes Totz:struct works, thanks! No need for class.class EnumType { int x; alias x this; this(int i) { x = i; } void opCall(int i) { } } enum X : EnumType { a = EnumType(1), b = 2 } Errors range are always variations of main.d(16): Error: function main.EnumType.opCall need 'this' to access member opCallThe error message suggests you to use a static opCall. And are you sure you want a class instead of a struct?
Nov 15 2011