COMMUNITY

Complex types as getter/setter function parameters and return values


(Chrisvelevitch) #1

I’m new to Haxe and I’ve come across this code:-

private var _myFunc:MyClass->Void;
public var myFunc(get, set):MyClass->Void;
private function get_myFunc():MyClass->Void { return _myFunc; }
private function set_myFunc(value:MyClass->Void):MyClass->Void
{
    ...
    return value;
}

How do I declare a value/function to use with the setter and how do I use the value/function from the getter?


(Remco Huijser) #2

Maybe this helps:

class Item
{
    private var _myFunc : Float->Void;

    public var myFunc(get, set) : Float->Void;

    private function get_myFunc() : Float->Void
    {
        return _myFunc;
    }

    private function set_myFunc(value : Float->Void) : Float->Void
    {
        return _myFunc = value;
    }

    public function new()
    {
    }
}

class Main
{
	static function main()
	{
		// Create instance.
		var item = new Item();

		// Set the function: this uses the setter function automatically.
		item.myFunc = function(value : Float)
		{
			trace(value);
		}

		// Call the function with a value.
		item.myFunc(5);
	}
}