Skip to content

Working with abstracts

ashley edited this page Aug 9, 2026 · 1 revision

Important

Abstracts have to be exposed with a build macro beforehand!

To get an "abstract" from a value, you can use cast...

var color:FlxColor = cast sprite.color; // cast(sprite.color, FlxColor) is valid, too!

color.red; color.green; color.blue; // this should all work after!

You should also be able to do implicit casts on function arguments and return values!

function getColorRed(color:FlxColor):Int { return color.red; }

trace(getColorRed(0xff0000)); // 255
trace(getColorRed(FlxColor.PURPLE)); // 128

Limitations

Although abstracts are supported in HscriptInsanity, they have a lot of technical limitations (unfortunately working around these limitations would be incredibly difficult).

One of the main limitations is that you must directly cast any value into an abstract. Setting a property from an abstract will also not actually modify the value in place, thus, you must set the variable directly after modifying said property.

cast(sprite.color, FlxColor).green = 255; // this will not change sprite.color...
sprite.color = (cast(sprite.color, FlxColor).green = 255); // ...so it must be directly set

To add on top of that, casting abstracts may be greatly degraded in some cases due to Haxe not enforcing type parameters at runtime.

Another current limitation is that operator overloading currently doesn't work; although, support is planned in the future...

Clone this wiki locally