Types: Casting

In Hack, variables can be cast to different types as normal, following the various set of pre-defined rules for casting.

NOTE: There is one modification to the rules. Remember, you cannot use the aliases for certain primitives in Hack (e.g, boolean instead of bool).


namespace Hack\UserDocumentation\Types\Inference\Examples\Unresolved;

function cast(): bool {
  $a = "10";
  $a = (int) $a;
  $a = (bool) $a;
  // $a = (float) $a; // Not allowed, a cast from bool to float
  return $a;