Syntactical Bug?

36 views
Skip to first unread message

Alec McEachran

unread,
Jul 9, 2013, 1:51:51 AM7/9/13
to haxe...@googlegroups.com
Hi again,

As part of my dust library (ongoing, early alpha) I have an entity system using macros to convert types to integers at compile-time, so that collections of components can be expressed as a bitfield. When you do something like:

entity.add(new Position()); for example, that is rewritten to entity.add(new Position(), 1); where 1 is the ID for Position across the application. Then entity.get(Position) is rewritten as entity.get(1); 

Annoyingly, I can also do:

entity.add(new QuadTree<Position>());
entity.add(new QuadTree<Rabbit>());

I would like QuadTree<Position> and QuadTree<Rabbit> to have two different IDs, but I can't write entity.get(QuadTree<Position>); I get the error "Unexpected )". I can only use entity.get(QuadTree), which means that the two generic types have to be treated the same in the indexer.

This is not a huge deal - I can work around it, but it seems to me like it's a bug that you can't reference a generic class like that - particularly if it's being passed into a macro function.

Nicolas Cannasse

unread,
Jul 9, 2013, 3:51:48 AM7/9/13
to haxe...@googlegroups.com
Le 09/07/2013 07:51, Alec McEachran a �crit :
Haxe don't have generics by default, which means that for each A and B,
MyClass<A> == MyClass<B>. That's only false when using @:generic. In all
cases, we don't support Class<T> syntax as an expression, only as a type.

Since you're in a macro, a workaround can be to use the following
syntax: get(QuadTree[Position])

Best,
Nicolas



Juraj Kirchheim

unread,
Jul 9, 2013, 5:12:05 AM7/9/13
to haxe...@googlegroups.com
Or (assuming the type parameters are always classes) you could do something like this:

    @:generic class QuadTree<T> {
        static public function of<T>(t:Class<T>):QuadTree<T> return new QuadTree<T>();
    }
And then `entity.add(QuadTree.of(Rabbit))`. The `@:generic` is only needed if you intend to distinguish the different quadtrees by type at runtime.

Regards,
Juraj
Reply all
Reply to author
Forward
0 new messages