Tuples: Relationship To Arrays

Under the covers, in HHVM, tuples are really just restricted arrays.

Elements

You cannot change a tuple's number of elements. You can change a value of a tuple. Prior to 3.27, it was required that elements retain the same type.

Reading

You can read from a tuple using array square bracket syntax. The most common way to read from a tuple, however, is to use list() assignment.

<?hh

namespace Hack\UserDocumentation\Tuples\RelationshipToArrays\Examples\ChgValue;

function run(): void {
  $t = tuple (3, "str", array(1, 2));
  \var_dump($t[1]); // literal syntax
  list($i, $s, $arr) = $t; // list assignment
  \var_dump($i);
  \var_dump($s);
  \var_dump($arr);
}

run();
Output
string(3) "str"
int(3)
string(3) "str"
array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}