repeating enum / enum alias

210 views
Skip to first unread message

Raoul Duke

unread,
Jul 15, 2015, 2:14:28 AM7/15/15
to haxe...@googlegroups.com
In horrible languages like C and C++ one can do something like this
horrible pseudocode. What various ways of doing something like this
can you suggest in Haxe? Thanks.

enum ThingTypes {
firstFoo = foo1,
foo1,
foo2,
foo3,
lastFoo = foo3,
firstBar = bar1,
bar1,
bar2,
bar3,
lastBar = bar3
};

Jason O'Neil

unread,
Jul 16, 2015, 8:54:17 PM7/16/15
to haxe...@googlegroups.com
I doubt you could make it work with real enums.  Using enum abstracts could work though:

http://try.haxe.org/#E93Ec

class Test {
    static function main() {
        trace("First foo:" + ThingTypes.firstFoo);
        trace("Bar 2:" + ThingTypes.bar2);
        trace("Last bar:" + ThingTypes.lastBar);
    }
}

@:enum abstract ThingTypes(String) from String to String {
  var firstFoo = foo1;
  var foo1 = "foo1";
  var foo2 = "foo2";
  var foo3 = "foo3";
  var lastFoo = foo3;
  var firstBar = bar1;
  var bar1 = "bar1";
  var bar2 = "bar2";
  var bar3 = "bar3";
  var lastBar = bar3;
}



--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Raoul Duke

unread,
Jul 16, 2015, 9:20:43 PM7/16/15
to haxe...@googlegroups.com
crazy!!! thanks. :-) (The next thing would be for them to have
indexes, since that's one nice thing about enums is they build up ids
for you.)

Benjamin Dubois

unread,
Jul 17, 2015, 12:41:24 AM7/17/15
to haxe...@googlegroups.com

Jason O'Neil

unread,
Jul 17, 2015, 12:52:01 AM7/17/15
to haxe...@googlegroups.com

You could change it to :

@:enum abstract ThingTypes(Int) from Int to Int {
  var firstFoo = foo1;
  var foo1 = 1;
  var foo2 = 2;
  var foo3 = 3;


  var lastFoo = foo3;
  var firstBar = bar1;

  var bar1 = 4;
  var bar2 = 5;
  var bar3 = 6;
  var lastBar = bar3;
}

Not the most convenient but will do the trick. It also keeps the runtime code /performance pretty clean. See

http://haxe.org/manual/types-abstract-enum.html

▶ Show quoted text

Raoul Duke

unread,
Jul 17, 2015, 3:17:21 AM7/17/15
to haxe...@googlegroups.com
> Not the most convenient but will do the trick. It also keeps the runtime
> code /performance pretty clean. See
> http://haxe.org/manual/types-abstract-enum.html


Thanks! All new things to learn for me. Ja, overall it seems still
tricky because ideally a main desire is for humans not to have to type
in #s at all. E.g. so you can insert a new thing and all the #s move
up one if need be, like with C enums. Overall probably the best way to
get what I had in mind is just something dirt simple-ish:

enum Foo { A, B1, B2, B3, C };
public inline var firstB = Type.indexOf( Foo.B1 );
public inline var lastB = Type.indexOf( Foo.B3 );

???
Reply all
Reply to author
Forward
0 new messages