HH\Map::toImmMap

Returns a deep, immutable copy (ImmMap) of the current Map

Description

public function toImmMap(): ImmMap<Tk, Tv>

Return Values

Examples

<?hh

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

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

function expects_immutable(ImmMap<string, string> $iv): void {
  var_dump($iv);
}

// Get a deep, immutable copy of $m
$immutable_map = $m->toImmMap();

expects_immutable($immutable_map);
Output
object(HH\ImmMap)#2 (4) {
  ["red"]=>
  string(7) "#ff0000"
  ["green"]=>
  string(7) "#00ff00"
  ["blue"]=>
  string(7) "#0000ff"
  ["yellow"]=>
  string(7) "#ffff00"
}