Tuple
A tuple is very similar to a PHP array and a shape. The key feature of tuples is that they are immutable. You may not change the length or the types of the elements in a tuple. The values within the tuple are mutable.
If you are trying to add more values to a tuple you will hear Hack complain, but the code will run on HHVM. The implementation of tuples is actually a pure array. This is the current implementation:
The best use case I can see for tuples are when you need to return multiple values from a function. Notice how I annotate the return type.
This is easier to write than using shapes for the same purpose.
Since a tuple is an immutable collection with fixed length, it kind of covers all the use cases for a Pair. The only difference is that a tuple is a data type and the Pair is an object. Why would you ever want to use a Pair when you can use the quick and easy solution with tuples?
Leave a Comment