HH\Vector::resize

Resize the current Vector

Description

public function resize(
  int $sz,
  Tv $value,
): void

Resize the current Vector to contain $sz elements. If $sz is smaller than the current size of the current Vector, elements are removed from the end of the current Vector. If $sz is greater than the current size of the current Vector, the current Vector is extended by appending as many copies of $value as needed to reach a size of $sz elements.

$value can be null.

If $sz is less than zero, an exception is thrown.

Parameters

  • $sz - The desired size of the current Vector.
  • $value - The value to use as the filler if we are increasing the size of the current Vector.

Examples

This example shows how resize can be used to decrease and increase the size of a Vector:

<?hh

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

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

// Resize the Vector to 2 (removing 'blue' and 'yellow')
$v->resize(2, null);
var_dump($v);

// Resize the Vector back to 4 (filling in 'unknown' for new elements)
$v->resize(4, 'unknown');
var_dump($v);
Output
object(HH\Vector)#1 (2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
}
object(HH\Vector)#1 (4) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(7) "unknown"
  [3]=>
  string(7) "unknown"
}