Hack

Updated: 2018-05-27

HSL

How to Convert mixed to Array

Use invariant

function f(mixed $a): array {
  invariant(is_array($a), 'Error, $a is not an array');
  return $a;
}

If it may be an array, or something else, simply make an if check:

function f(mixed $a):void {
  if (is_array($a)) {
    // You can use $a as an array now.
    $a[0] = 'a';
  } else {
    // Here it's still mixed
  }
}

How to Return Different Types

Use Either:

Awaitable<Either<ConstVector<MyObject>, string>>

Hack Arrays

  • Hack Arrays: vec, dict, keyset
  • Hack Collections: Vector, Map, Set
  • PHP Arrays: array

Preference: Hack Arrays > Hack Collections > PHP Arrays