www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum with classes/structs

reply useo <useo start.bg> writes:
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
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
prev sibling next sibling parent Trass3r <un known.com> writes:
 ... or does enumerations only support constant-expressions? Thanks!

Yep, enum is a compile-time thing.
Mar 10 2011
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, March 10, 2011 11:28:04 bearophile wrote:
 useo:
 is it possible to declare a enum where all entries are instances of a

 class (or struct), like the following:

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.

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 Davis
Mar 10 2011
parent useo <useo start.bg> writes:
== Auszug aus Jonathan M Davis (jmdavisProg gmx.com)'s Artikel
 On Thursday, March 10, 2011 11:28:04 bearophile wrote:
 useo:
 is it possible to declare a enum where all entries are



 class (or struct), like the following:

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-


 are often separated. typedef didn't work well with OOP. Don't


 that are not meant to be mixed.


 enumeration of values. There's absolutely nothing wrong with

 of struct or class types. The only restrictions there are

 implementation. TDPL even gives examples of enum structs. They

 when you only have one value in the enum, but fail when you have

 http://d.puremagic.com/issues/show_bug.cgi?id=4423 ). If/When

 CTFE, then you should be able to haveenums of class objects.
 There's nothing about enums which are C or procedural-specific.

 oriented enums which are quite powerful. And aside from the

 implementation issues, D's enums are even more powerful because

 type and so can be either primitive types or user-defined types

 in Java.
 enums don't care one way or another about OOP. They're just a set

 have to be ordered and be known at compile time.
 - Jonathan M Davis

Okay, 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
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
*I mean construction via field-by-field assignment. (I made the same
typo in the bug report, lol).
Mar 10 2011