Using the Arrow operator in Haxe and other variable type issues

I am following a tutorial for HaxeFlixel that uses the Haxe language. Now I have no experience with Haxe, but I decided to dare to take this lesson, since I have experience with Java and Haxe, as the language seems strangely similar to Java.

So far, everything went smoothly. However, I came across this piece of code, and I have a few questions:

class FSM { public var activeState:Void->Void; public function new(?InitState:Void->Void):Void { activeState = InitState; } public function update():Void { if (activeState != null) activeState(); } } 

Now I understand that this is a class called FSM and has a variable called activeState .

Here are my questions:

  • What is the type of activeState ? I would understand if there was something like activeState:Void , but what does -> do? Is it used as a pointer? Is it a pointer to void pointing to another void variable?

  • What does it mean ? before InitState:Void->Void ?

  • After the if statement, activeState is called as a function using parentheses. However, activeState is a variable, not a function. So what exactly does the if statement do?

One more question:

 public var playerPos(default, null):FlxPoint; 

I understand that playerPos is an instance of the FlxPoint class, but what do default and null do?

+5
source share
1 answer
  • The type Void->Void is a type of function , in this case a function that takes no arguments and returns Void .

  • ? indicates an optional argument . In this case, this is equivalent to writing new(InitState:Void->Void = null) .

  • activeState is a variable, but it stores a function - you guessed it, activeState() calls it.

(default, null) indicates that playerPos is property . With default as the read access identifier and null as the write access identifier, it is read-only outside the class in which it is defined.

+9
source

Source: https://habr.com/ru/post/1236567/


All Articles