It is possible to do implicit casting within a class and not abstracts?

57 views
Skip to first unread message

funplo...@gmail.com

unread,
Jun 17, 2017, 3:14:45 AM6/17/17
to Haxe
I want to make a class, that I can use implicit casting in like in an Abstract:

var a:MyInt = 5;
a
.name = "some name";
a
.multiplier = 1.3;
a
.referenceToSomething = object;

Where MyInt is a Class.

The reason I want to use classes and not abstracts is because MyInt would have fields like "name", "multiplier" etc. And if I cast them through an abstract - those other values would be lost.

Is it possible to create such a type?

Gama11

unread,
Jun 17, 2017, 5:25:10 AM6/17/17
to Haxe
No, like operator overloading, implicit casts are only a thing for abstracts.

You could have an abstract that wraps your MyInt class.

funplo...@gmail.com

unread,
Jun 17, 2017, 6:36:57 AM6/17/17
to Haxe
My abstract is currently wrapping the MyInt class, but if I set a new value via "@:from" method, all other properties will be lost. I'm trying to find a way how to keep other values too.

Gama11

unread,
Jun 17, 2017, 9:30:47 AM6/17/17
to Haxe
I don't think you can do that - @:from creates a new instance / doesn't know anything about any old instances there might be.

funplo...@gmail.com

unread,
Jun 18, 2017, 2:22:01 AM6/18/17
to Haxe
Exactly! Thus my post :)

I need to find an elegant way to solve that problem or somehow self-contain it in a black box.
Reply all
Reply to author
Forward
0 new messages