Extending enums

629 views
Skip to first unread message

Horsetopus

unread,
Jan 28, 2012, 8:09:17 AM1/28/12
to haxe...@googlegroups.com
Hi people,

I was wondering, is there any way to extend enum types?

For example, Class1 has 3 fields, and I want Class2 to have those fields and one more.

It is not much to write the second class all over again, but it would still be nice in some cases.

I'm pretty sure the answer is no, since the compiler didn't accept neither "extends" nor "implements", but maybe there is some sort of trics?

Thanks.

Mihail Ivanchev

unread,
Jan 28, 2012, 8:13:15 AM1/28/12
to haxe...@googlegroups.com
Nope, no way to do it. You'll have to change the architecture or simulate the extension with some other means.

Regards,
M. Ivanchev

Mihail Ivanchev

unread,
Jan 28, 2012, 8:14:09 AM1/28/12
to haxe...@googlegroups.com
Of course, you can always do what Rob Fell does in the awe6 framework:

enum Foo{
     A;
     B;
     C;
     SUB_TYPE( value: Dynamic ); // Use with custom values to extend.

Simon Krajewski

unread,
Jan 28, 2012, 8:18:19 AM1/28/12
to haxe...@googlegroups.com
Am 28.01.2012 14:13, schrieb Mihail Ivanchev:
> Nope, no way to do it.

I'm willing to bet you can pull off something along the lines of

@:build(pack.BuildMacro.extendEnum(BaseEnum)) enum ChildEnum
{
NewConst;
}

See http://haxe.org/manual/macros/build

Simon

Nicolas Cannasse

unread,
Jan 28, 2012, 8:43:30 AM1/28/12
to haxe...@googlegroups.com
Le 28/01/2012 14:09, Horsetopus a �crit :

The common way to extend an enum is the to do the following :

enum First {
A;
B;
C;
}

enum Second {
F( f : First );
D;
E;
F;
}

This way you can do use "D" or "F(B)", and it still respect the type system.

Best,
Nicolas

aurore...@gmail.com

unread,
Jan 28, 2012, 3:18:12 PM1/28/12
to haxe...@googlegroups.com
Still, it requiers two level pattern matching (switch) to destructure... until the nested switchs works :)

Any progress on that side ( and GADTs ??? )

Stephane

Juraj Kirchheim

unread,
Jan 28, 2012, 6:28:54 PM1/28/12
to haxe...@googlegroups.com
On Sat, Jan 28, 2012 at 2:14 PM, Mihail Ivanchev <miha...@gmail.com> wrote:
> Of course, you can always do what Rob Fell does in the awe6 framework:
>
> enum Foo{
>      A;
>      B;
>      C;
>      SUB_TYPE( value: Dynamic ); // Use with custom values to extend.
> }

For the sake of type safety, I suggest a slight enhancement:

     enum Foo<T> {
          A;
          B;
          C;
          SUB_TYPE(value:T); // Use with custom values to extend.
     }

And then T can for example be an enum.

Regards,
Juraj

Horsetopus

unread,
Jan 29, 2012, 8:24:25 AM1/29/12
to haxe...@googlegroups.com
Ok, thank you all.
That works perfectly.
Reply all
Reply to author
Forward
0 new messages