use of haxe.extern.EitherType

65 views
Skip to first unread message

Żabojad

unread,
Jan 8, 2016, 1:05:38 PM1/8/16
to Haxe
Hi,

I'm currently writing an extern for a js lib.

here is a part of it:
typedef Struct1 = {
field11 : String
}

typedef Struct2 = {
field21 : String,
? field22 : String
}

@:native("somelib")
extern class SomeLib {
public static function doSomething(ev : String, cb : EitherType<Struct1, Struct2> -> Void) : Void;
}

And somewhere in my app code:

SomeLib.doSomething('anEvent', function(d) {

           trace('event field11: ${rd.field11}');
       });


This con't compile with the following error:  haxe.extern.EitherType<Struct1, Struct2> has no field field11

Can't we use haxe.extern.EitherType with structures ?

Żabojad

unread,
Jan 8, 2016, 1:09:22 PM1/8/16
to Haxe
Note that specifying the type in the callback definition makes it pass the compilation:

SomeLib.doSomething('anEvent', function(d: Struct1) {

                    trace('event field11: ${rd.field11}');
             });

So actually, it's OK... It's just type inference that doesn't work here. It might not be a bug...

Porfírio Ribeiro

unread,
Jan 9, 2016, 7:51:50 PM1/9/16
to Haxe
I don't think it is a bug...

d is inferred to be either Struct1 or Struct2, but the compiler don't know which one have been passed to the callback!

You are assuming it was Struct1 and it might be, but if it is Struct2 it would give a runtime error.
Reply all
Reply to author
Forward
0 new messages