HH\Map::toImmVector

Returns an immutable vector (ImmVector) with the values of the current Map

Description

public function toImmVector(): ImmVector<Tv>;

Return Values

  • ImmVector<Tv> - an ImmVector that is an immutable copy of the current Map.

Examples

This example shows that toImmVector returns an immutable copy of the Map's values. Mutating the Vector of values doesn't affect the original Map and vice-versa.

<?hh

namespace Hack\UserDocumentation\API\Examples\Map\ToImmVector;

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
};

function expects_immutable(ImmVector<string> $iv): void {
  \var_dump($iv);
}

// Get an immutable Vector of $m's values
$immutable_v = $m->toImmVector();

// Add a color to the original Map $m
$m->add(Pair {'purple', '#663399'});

expects_immutable($immutable_v);
Output
object(HH\ImmVector)#2 (4) {
  [0]=>
  string(7) "#ff0000"
  [1]=>
  string(7) "#00ff00"
  [2]=>
  string(7) "#0000ff"
  [3]=>
  string(7) "#ffff00"
}