genericBuild type parameters with metadata

20 views
Skip to first unread message

Munir Hussin

unread,
Apr 23, 2016, 10:32:11 AM4/23/16
to Haxe
I'm playing around with haxe macros to try making a Tuple type for fun. So far I've gotten it to work.

var t = new Tuple<Int, String>(5, "hello");
t
.v0; // 5
t
.v1; // hello

// accepts any number of type parameters
var t = new Tuple<Int, String, Bool>(5, "hello", false);
var t = Tuple.of(5, "hello", false); // macro that generates line above

// generates: abstract Tuple3<T0,T1,T2>(Array<Dynamic>) { public var v0(get, set):T0; ... }


All the above works. Then I remembered python have named tuples, so I thought of adding that into the macro, so instead of t.v0, t.v1 etc... you can use actual variable names.

var t = new Tuple<@foo Int, String, Bool>(5, "hello", false);
// generates: @:forward abstract NamedTuple3:foo::<T0,T1,T2>(Tuple3<T0,T1,T2>) { public var foo(get, set):T0; ... }
t
.foo; // 5
t
.v0;  // 5 (forwarded property)

Alright, so I got the above to work too. Then when I tested with multiple meta...

var t = new Tuple<@foo Int, @bar String, Bool>(5, "hello", false);      // works :)
var t = new Tuple<@foo Int, @bar String, @baz Bool>(5, "hello", false); // compile error :(
// --------------------------------------------------^ Unexpected ,

It appears that when a meta is at the last type parameter, there'll be a parse error.
Any suggestions for an alternate expr where I could annotate a type with a string/identifier in type parameters that doesn't result in parsing errors?

Reply all
Reply to author
Forward
0 new messages