Working with abstracts - inky03/hscript-insanity GitHub Wiki
[!IMPORTANT] Abstracts have to be exposed with a build macro beforehand!
To turn a value into an "abstract", 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 afterwards!
You can also implicitly cast values on function arguments and return values!
function getColorRedChannel(color:FlxColor):Int { return color.red; }
trace(getColorRedChannel(0xff0000)); // 255
trace(getColorRedChannel(FlxColor.PURPLE)); // 128
Features such as operator overloading (including array and field resolving) are supported, so you might be able to use them with some precautions!
var colorAddition = (FlxColor.RED + FlxColor.BLUE);
trace(colorAddition.toHexString()); // 0xFFFF00FF
Limitations
Although abstracts are supported in HscriptInsanity, a lot of technical limitations are present.
-
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 setCasting types to abstracts and viceversa may also be degraded in some cases due to Haxe not enforcing type parameters at runtime.
-
Due to implementation reasons...
- There may be some edge cases where return values, or modifications of the abstract in place, may not be exact with Haxe. This may be remedied in the future.
- Postincrement
(A ++)and postdecrement(A --)are currently unsupported when working with source code abstracts (scripted abstracts will work). An error will be thrown if there's an attempt to use them. This will be fixed in the future.