Thread overview
enum with classes/structs
Mar 10, 2011
useo
Mar 10, 2011
Simen kjaeraas
Mar 10, 2011
Trass3r
Mar 10, 2011
bearophile
Mar 10, 2011
Jonathan M Davis
Mar 11, 2011
useo
Mar 10, 2011
Jonathan M Davis
Mar 10, 2011
Andrej Mitrovic
Mar 10, 2011
Andrej Mitrovic
March 10, 2011
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!
March 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
March 10, 2011
> ... or does enumerations only support constant-expressions? Thanks!

Yep, enum is a compile-time thing.
March 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
March 10, 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
March 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.
March 10, 2011
*I mean construction via field-by-field assignment. (I made the same
typo in the bug report, lol).
March 10, 2011
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:
> 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.

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
March 11, 2011
== 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
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.
> 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

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.