Using SWC libraries created with OpenFL/HAXE: no read-only values?

Hi, I’m creating a project with OpenFL targeting at Flash and HTML5 for browser display. I will also need to use the code I’m creating on AIR-based apps for Android, iOS, Windows and OSX, so I’m also exporting the code as an SWC library to be imported on Apache Flex.

Everything works fine unless I use read-only values, like this:

public var loading(get, null):Bool;
public function get_loading():Bool
    return (this._loading);

If I use the exact code above, the Apache Flex project doesn’t get the right value for the variable (always false for Bool, undefined for String and so on). If I try to use @:getter like on getters/setters, like the code below, the SWC is created correctly but when I start the app I get an stranage error: The ABC data is corrupt, attempt to read out of bounds.

public var loading(get, null):Bool;
public function get_loading():Bool { return (this._loading); }

Is there a way have read-only values on my code? Oh, full getters/setters work as expected.

Try this:

public var loading(get, never):Bool;

If that doesn’t work, you could try deleting the line entirely. It’s possible that as long as you have “@:getter(loading),” you don’t need a variable declaration.

Other than that, I’d suggest making a setter that throws an error.

Thank you a lot, @player_03! Using (get, never) worked perfectly!

An update: the (get, never) does not work correctly on the Flash target exported directly from OpenFL. As @player_03 suggested, I tested and noticed that these declarations are not needed at all to run the code on Flash (directly from OpenFL or from the SWC) but are required for the HTML5 target. So, the solution I found was adding a #if !flash directive to the read-only and getter+setter declarations.

A curious bug: this works if the class extends openfl.display.Sprite. I tried on a class extending and the getters/setters just don’t work on Flash, exported as swc or even compiled directly from OpenFL. I keep getting an error saying that the property is not set.

Are you using @:getter and @:setter? Those create “real” Flash getters and setters

Yes, I’m using both. If the class extends any of the DisplayObject subclasses, the getters/setters (using @:getter and @:setter) work as expected, but if the class does not extend anything or extends EventDispatcher, I keep getting an error about get_[property name] not being set, like

src/Main.hx:157: characters 9-18 : has no field id

This happens only on Flash target. On HTML5 everything works fine.

If you’re defining the variable yourself, you can skip the @:getter tag entirely. Just declare it as (get, null) or (get, never), and include a matching get_ function. You’ll be able to override this function normally.