Hey everyone, recently in 4.2, they added an awesome new feature to the Haxe programming language. Rest has been added to Haxe. If you worked in JavaScript, Reason, or some other languages, you’ve seen the … operator before; that’s rest and Haxe now supports it as a proper type. But, how can you use it in order to gain some advantage in your code?
How To Use Rest
Unlike JavaScript, Haxe rest is a bit more limited in its usefulness. You can’t just use it as a drop-in replacement for regular function arguments where an array would have been accepted earlier. You also can’t use it to push multiple elements into an array at once like in JavaScript. However, it does let you create functions that can take any number of arguments without having to create an array. This is very powerful because it allows you to now create variadic functions in Haxe which would have required you to use an array before. Now, we can create more robust APIs in Haxe. With that said, you’ll find some examples in the next section.
Advantages Of Rest
Rest allows you to create additive functions, variadic functions that were never available before in Haxe without using an array or using some of the functions tools. For example, with rest, you can write a summing function that takes any amount of numbers, strings, and more. You could do the same thing for numbers with multiplication. See the example below.
Another example use for this is to create a piping function. A piping function is a function that takes a value and applies each function individually to the value. Here’s an example.
As you can see the value just gets piped through each function until you finally reach the end. This can be very useful in functional programming.
There are many more use cases for Rest in Haxe. I feel we’ve barely scratched the surface. Hopefully, you find these useful in your own code. I know I’ll start using them in my own game in order to make things easier for myself for creating robust APIs!
For additional references you can find the API for Rest here.