HH\Set::toImmVector

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

Description

public function toImmVector(): ImmVector<Tv>;

Return Values

  • ImmVector<Tv> - an ImmVector (integer-indexed) with the values of the current Set.

Examples

This example shows that toImmVector returns an ImmVector containing the Set's values. Mutating the original Set doesn't affect the ImmVector.

<?hh

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

$s = Set {'red', 'green', 'blue', 'yellow'};

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

// Get an immutable Vector $v of the values in Set $s
$immutable_v = $s->toImmVector();

// Add a color to the original Set $s
$s->add('purple');

expects_immutable($immutable_v);
Output
object(HH\ImmVector)#2 (4) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(4) "blue"
  [3]=>
  string(6) "yellow"
}