Help with understanding Haxe language

Hi!
I am newbie in Haxe. I try to port my as3 Starling game to OpenFl/Starling.
I need some help to understand Haxe)
This is a part of my game windows manager. It works on as3. I cut all unnecessary code.

class WindowType
{
	public var _name:String;
	static public final WINDOW_BATTLE_INFO = new WindowType("WINDOW_BATTLE_INFO");
	static public final WINDOW_CONFIRM = new WindowType("WINDOW_CONFIRM");

    public function new(name:String):Void
    {
        _name = name;
    }

    public function toString():String
    {
        return _name;
    }
}
class BaseWindow extends Sprite implements IWindow
{
	public var Type:WindowType;
	function get_Type():WindowType
	{		
		throw ("must be overriden!");
	}
}
class ConfirmWindow extends BaseWindow
{
	override function get_Type():WindowType
	{
		return WindowType.WINDOW_CONFIRM;
	}
}
class MainClass extends Sprite
{
	private var _currentWindows = new Map<String, BaseWindow>();

	public new():Void
	{
		super();
		showWindow(WindowType.WINDOW_CONFIRM);
	}

	public function showWindow(type:WindowType):Void
	{
		var mcWindow:BaseWindow = WindowsFactory.getWindow(type);
		mcWindow.addEventListener(Event.ADDED_TO_STAGE, onWindowAddedChangeListener);
		
		this.addChild(mcWindow);
	}
	
	private function onWindowAddedChangeListener(e: Event): Void
	{
		var win:BaseWindow = cast e.currentTarget;	// I check that 'win' is 'ConfirmWindow'		
		_currentWindows[win.Type.toString()] = win;	// Uncaught TypeError: Cannot read properties of null (reading 'toString')	
	}
}

I get win.Type == null in last string. Why?)

The current code you have Type is a regular variable, not a getter. And it’s not set to anything, so it’s throwing a null ref error.

So instead of this:

public var Type:WindowType;
function get_Type():WindowType
{		
	throw ("must be overriden!");
}

You need to do this:

public var Type(get, null):WindowType
function get_Type():WindowType
{		
	throw ("must be overriden!");
}
1 Like

Thx!
I am stupid)

What is difference in ‘null’ and ‘never’?
public var Type(get, null/never):WindowType

null means that you can set the variable from inside the class only. You can’t set it from outside the class. never means that you can’t set it under any circumstances.

1 Like