HH\Set::__construct

Description

public function __construct(
  ?Traversable<Tv> $it,
): void

Creates a Set from the given Traversable, or an empty Set if null is passed.

Parameters

  • ?Traversable<Tv> $it - any Traversable object from which to create the Set (e.g., array). If null, then an empty Set is created.

Examples

This example shows how to create a Set from various Traversables. Notice that duplicate values in the input Traversables only appear once in the output Set.

<?hh

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

// Create a new Set from an array
$s = new Set(array('red', 'green', 'red', 'blue', 'blue', 'yellow'));
var_dump($s);

// Create a new Set from a Vector
$s = new Set(Vector {'red', 'green', 'red', 'blue', 'blue', 'yellow'});
var_dump($s);

// Create a new Set from the values of a Map
$s = new Set(Map {
  'red1' => 'red',
  'green' => 'green',
  'red2' => 'red',
  'blue1' => 'blue',
  'blue2' => 'blue',
  'yellow' => 'yellow',
});
var_dump($s);
Output
object(HH\Set)#1 (4) {
  string(3) "red"
  string(5) "green"
  string(4) "blue"
  string(6) "yellow"
}
object(HH\Set)#2 (4) {
  string(3) "red"
  string(5) "green"
  string(4) "blue"
  string(6) "yellow"
}
object(HH\Set)#3 (4) {
  string(3) "red"
  string(5) "green"
  string(4) "blue"
  string(6) "yellow"
}

This example shows how passing null to the constructor creates an empty Set:

<?hh

namespace Hack\UserDocumentation\API\Examples\Set\__construct\FromNull;

// An empty Set is created if null is provided
$s = new Set(null);
var_dump($s);
Output
object(HH\Set)#1 (0) {
}