Search code examples
haxe

Local constants in Haxe - possible


I know there is this question Constants in Haxe about class properties. My question is: is it possible to define constants inside functions? Like:

function foo(){
   const bar=7;
   bar = 8; // should prevent compilation
}

Maybe anything like var var foo:ReadOnlyInt or something?


Solution

  • You're looking for final.

    function foo() {
       final bar = 7;
       bar = 8; // not allowed
    }
    

    https://haxe.org/manual/expression-var.html