Types: Runtime

There are cases when the typechecker will report an error, but the program will run perfectly fine in HHVM.

<?hh

namespace Hack\UserDocumentation\Types\Runtime\Examples\Runs;

// Even though we specify that the function is void, HHVM will still allow
// us to return an int with no problem.
function foo(int $x): void {
  return $x * 2;
}

var_dump(foo(2));
Output
int(4)

However, there is some support for runtime type checking in HHVM, but its enforcement is currently limited.

  • HHVM ignores property annotations.
  • HHVM supports parameter and return type annotations; generally, if you violate the agreement, a catchable fatal error will be thrown. However there are exceptions:
    • void is not enforced at runtime; i.e., you can return a value from a void function at runtime
    • Generics are enforced as if they did not have type parameters.
    • Shapes and tuples are only enforced as if they were an array(). The inner types of each are not enforced.
    • Enums are enforced only at the underlying type level. HHVM does not check for valid enum values.
    • If you specify the soft-type hint operator @ before an annotation, a warning would be thrown in the case where a catchable fatal would have been.