Search code examples
haxe

What are the uses of an underscore in Haxe?


What are the uses of the underscore in Haxe?

I see that I can use it in loops and in array and map comprehensions when I don't care what the counter is, for example:

var a = [for (_ in 0...5) Math.random()]; // 5 random nums

Are there any other places where it's commonly used?


Solution

  • It's generally to denote values that indeed exist, but are not used in the code. Other uses would include:

    1. function arguments that are not used:

      button.addListener('click', function (_) trace('clicked!'));
      
    2. enum constructor arguments that are ignored:

      var o = haxe.ds.Option.Some(5);
      switch o {
        case None: trace('no value');
        case Some(_): trace('some value');
      }