HH\Vector::toVector

Returns a copy of the current Vector

Description

public function toVector(): Vector<Tv>

Return Values

Examples

This example shows how toVector() returns a copy of $v (a new Vector object), so mutating this new Vector doesn't affect the original.

<?hh

namespace Hack\UserDocumentation\API\Examples\Vector\ToVector;

$v = Vector {'red', 'green', 'blue', 'yellow'};

// Make a new Vector that is a copy of $v (i.e. contains the same elements)
$v2 = $v->toVector();

// Modify $v2 by adding an element
$v2->add('purple');
var_dump($v2);

// The original Vector $v doesn't include 'purple'
var_dump($v);
Output
object(HH\Vector)#2 (5) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(4) "blue"
  [3]=>
  string(6) "yellow"
  [4]=>
  string(6) "purple"
}
object(HH\Vector)#1 (4) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(4) "blue"
  [3]=>
  string(6) "yellow"
}

This example shows how toVector() returns a shallow copy of $v (a new Vector object containing the same elements) rather than a deep copy (a new Vector object containing copies of the elements of $v that are themselves objects).

Thus, mutating an element of $v that is itself an object also mutates the corresponding element of $v2, since the element in $v is the same object as the element in $v2.

<?hh

namespace Hack\UserDocumentation\API\Examples\Vector\ToVector;

$inner = Vector {1, 2, 3};
$v = Vector {'a', $inner, 'c'};

// Make a Vector copy of $v
$v2 = $v->toVector();

// Modify the original Vector $v's inner Vector by adding an element
$v[1]->add(4);

// The original Vector $v's inner Vector includes 4.
var_dump($v);

// The new Vector $v2's inner Vector also includes 4. toVector() only does a shallow copy.
var_dump($v2);
Output
object(HH\Vector)#2 (3) {
  [0]=>
  string(1) "a"
  [1]=>
  object(HH\Vector)#1 (4) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
    [3]=>
    int(4)
  }
  [2]=>
  string(1) "c"
}
object(HH\Vector)#3 (3) {
  [0]=>
  string(1) "a"
  [1]=>
  object(HH\Vector)#1 (4) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
    [3]=>
    int(4)
  }
  [2]=>
  string(1) "c"
}