digitalmars.D.learn - enum with classes/structs
- useo (15/15) Mar 10 2011 Hey guys,
- Simen kjaeraas (4/19) Mar 10 2011 Only constant expressions, sorry.
- Trass3r (1/2) Mar 10 2011 Yep, enum is a compile-time thing.
- bearophile (14/16) Mar 10 2011 I don't think so. Enums are compile-time constants.
- Jonathan M Davis (16/35) Mar 10 2011 There's absolutely nothing wrong with mixing enum with OOP. An enum is s...
- useo (18/53) Mar 11 2011 style features
- Jonathan M Davis (23/42) Mar 10 2011 enums _definitely_ only support constants. However, anything which is do...
- Andrej Mitrovic (3/3) Mar 10 2011 There's also this bug where the constructor for a struct isn't called:
- Andrej Mitrovic (2/2) Mar 10 2011 *I mean construction via field-by-field assignment. (I made the same
Hey guys, is it possible to declare a enum where all entries are instances of a class (or struct), like the following: class a { ... public this(uint i) { ... } ... } enum myEnum : a { entry1 = new a(0); entry2 = new a(1); } ... or does enumerations only support constant-expressions? Thanks!
Mar 10 2011
useo <useo start.bg> wrote:Hey guys, is it possible to declare a enum where all entries are instances of a class (or struct), like the following: class a { ... public this(uint i) { ... } ... } enum myEnum : a { entry1 = new a(0); entry2 = new a(1); } ... or does enumerations only support constant-expressions? Thanks!Only constant expressions, sorry. -- Simen
Mar 10 2011
... or does enumerations only support constant-expressions? Thanks!Yep, enum is a compile-time thing.
Mar 10 2011
useo:is it possible to declare a enum where all entries are instances of a class (or struct), like the following:I don't think so. Enums are compile-time constants. This code doesn't compile: class A { this(uint i) {} } enum myEnum : A { entry1 = new A(0), entry2 = new A(1) } void main() {} It's important to understand that in D OOP and procedural/C-style features are often separated. typedef didn't work well with OOP. Don't mix things that are not meant to be mixed. Bye, bearophile
Mar 10 2011
On Thursday, March 10, 2011 11:28:04 bearophile wrote:useo:There's absolutely nothing wrong with mixing enum with OOP. An enum is simply an enumeration of values. There's absolutely nothing wrong with those values being of struct or class types. The only restrictions there are problems with the implementation. TDPL even gives examples of enum structs. They currently work when you only have one value in the enum, but fail when you have multiple ( http://d.puremagic.com/issues/show_bug.cgi?id=4423 ). If/When classes work with CTFE, then you should be able to haveenums of class objects. There's nothing about enums which are C or procedural-specific. Java has object- oriented enums which are quite powerful. And aside from the current implementation issues, D's enums are even more powerful because they allow _any_ type and so can be either primitive types or user-defined types like you'd have in Java. enums don't care one way or another about OOP. They're just a set of values that have to be ordered and be known at compile time. - Jonathan M Davisis it possible to declare a enum where all entries are instances of aclass (or struct), like the following:I don't think so. Enums are compile-time constants. This code doesn't compile: class A { this(uint i) {} } enum myEnum : A { entry1 = new A(0), entry2 = new A(1) } void main() {} It's important to understand that in D OOP and procedural/C-style features are often separated. typedef didn't work well with OOP. Don't mix things that are not meant to be mixed.
Mar 10 2011
== Auszug aus Jonathan M Davis (jmdavisProg gmx.com)'s ArtikelOn Thursday, March 10, 2011 11:28:04 bearophile wrote:instances of auseo:is it possible to declare a enum where all entries arestyle featuresclass (or struct), like the following:I don't think so. Enums are compile-time constants. This code doesn't compile: class A { this(uint i) {} } enum myEnum : A { entry1 = new A(0), entry2 = new A(1) } void main() {} It's important to understand that in D OOP and procedural/C-mix thingsare often separated. typedef didn't work well with OOP. Don'tenum is simply anthat are not meant to be mixed.There's absolutely nothing wrong with mixing enum with OOP. Anenumeration of values. There's absolutely nothing wrong withthose values beingof struct or class types. The only restrictions there areproblems with theimplementation. TDPL even gives examples of enum structs. Theycurrently workwhen you only have one value in the enum, but fail when you havemultiple (http://d.puremagic.com/issues/show_bug.cgi?id=4423 ). If/Whenclasses work withCTFE, then you should be able to haveenums of class objects. There's nothing about enums which are C or procedural-specific.Java has object-oriented enums which are quite powerful. And aside from thecurrentimplementation issues, D's enums are even more powerful becausethey allow _any_type and so can be either primitive types or user-defined typeslike you'd havein Java. enums don't care one way or another about OOP. They're just a setof values thathave to be ordered and be known at compile time. - Jonathan M DavisOkay, thanks - I'll hope the bug will be solved. I'm absolution right here with you, the enumerations of Java are very use- and powerful.
Mar 11 2011
On Thursday, March 10, 2011 11:15:25 useo wrote:Hey guys, is it possible to declare a enum where all entries are instances of a class (or struct), like the following: class a { ... public this(uint i) { ... } ... } enum myEnum : a { entry1 = new a(0); entry2 = new a(1); } ... or does enumerations only support constant-expressions? Thanks!enums _definitely_ only support constants. However, anything which is doable with CTFE should work with an enum as long as the result is const or immutable (or convertible to const or immutable). So, it _should_ be possible to have an enum where all of its values are structs. And something like this works: struct S { ... } enum val = S(24); However, for some reason, having an enum of a struct type with multiple values doesn't currenty work (I believe that it relates to comparison - when you have multiple values in an enum, they have to be comparable, because they're ordered): http://d.puremagic.com/issues/show_bug.cgi?id=4423 Classes do not currently work with CTFE, so they _definitely_ don't work. However, the goal is that eventually everything which is legal in SafeD will work with CTFE, and if/when we reach that point, then it would be legal to have an enum of class objects. However, _regardless_ of the type of an enum, its values can't be changed, so even if you had a struct or class object as an enum value, you wouldn't be able to change its value or call any non-const functions on it. - Jonathan M Davis
Mar 10 2011
There's also this bug where the constructor for a struct isn't called: http://d.puremagic.com/issues/show_bug.cgi?id=5460 , and field assignment is not disabled even with the presence of a constructor.
Mar 10 2011
*I mean construction via field-by-field assignment. (I made the same typo in the bug report, lol).
Mar 10 2011