Type constrains outside of type parameters

56 views
Skip to first unread message

Haxiomic

unread,
Dec 15, 2015, 7:41:48 AM12/15/15
to Haxe
Hello

I'm a big fan of type constraints in type parameters and as far as I'm aware constraints only work for type parameters but is there anything preventing them being available to constrain types elsewhere?

For example, I've got a class with a static field 'Camera', it needs to be of type three.Camera but it also needs to have fields 'near' and 'far'. (Which not all subclasses of three.Camera have)

It feels like the simplest thing to do would be to define the field as:

static var camera:(three.Camera, {near:Float,far:Float}));

which I could of course do if I was using a type parameter, but because it's a static field, I don't believe I can use one

Is there another way of achieving this? Or is it worth putting forward as a feature suggestion?

Thanks,
George
Message has been deleted

Ben Merckx

unread,
Dec 15, 2015, 5:46:03 PM12/15/15
to Haxe
You could try this:
static var camera: {>three.Camera, near:Float, far:Float};

Haxiomic

unread,
Dec 15, 2015, 5:52:43 PM12/15/15
to Haxe
Thanks for the tip Ben, I didn't know that was permitted! Bad news however, I get

"Structurally extending classes is deprecated and will be removed" (haxe 3.3)

Rats. That would have been just what I was looking for.

Ben Merckx

unread,
Dec 15, 2015, 6:07:18 PM12/15/15
to Haxe
Figures, I have seen that warning before, but wasn't exactly sure anymore. Tried it on try.haxe.org without errors, but I guess dce removed the code before the warning. I don't really know of another simple solution though.
Reply all
Reply to author
Forward
0 new messages