Arrow operator usage in Haxe and other variable type related questions
Asked Answered
D

1

5

I am following a tutorial for HaxeFlixel which uses the Haxe language. Now I do not have any experience in Haxe, but I decided to brave the tutorial since I do have experience in Java and Haxe as a language seems weirdly similar to Java.

So far, it was going smoothly. However, I came across this piece of code and I have several 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:

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

  2. What does the ? before the InitState:Void->Void signify?

  3. After the if statement, the activeState is being called like a function using the parentheses. However, activeState is a variable and not a function. So what does the if statement do exactly?

Also another question:

public var playerPos(default, null):FlxPoint;

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

Dna answered 22/11, 2015 at 20:0 Comment(0)
E
9
  1. The type is Void->Void - it's a function type, in this case a function that takes no arguments and returns Void.

  2. ? indicates an optional argument. In this case it's equivalent to writing new(InitState:Void->Void = null).

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

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

Encumber answered 22/11, 2015 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.