2

Why is this code working?

<?hh // strict
function test(Vector<int> $v):void {
    print_r($v);
}

test(Vector {1, array("I'm an array"), 3});

Shouldn't it throw an error? What is the <int> supposed to be for?

Cequiel
  • 3,505
  • 6
  • 27
  • 44

1 Answers1

7

This won't throw an error in HHVM, but will in the Hack tools. This is due to HHVM currently ignoring generics, so it is just checking if $v is a Vector.

Running the Hack tools (hh_client) will first complain about the top level statements and, if you correct that by wrapping the call to test inside a function, will correctly complain about trying to pass a Vector<mixed> as a Vector<int>.

  • 2
    Relevant documentation on how to get `hh_client` set up is here: http://docs.hhvm.com/manual/en/install.hack.bootstrapping.php As additional color, note that type erasure semantics for generics aren't the way things will necessarily work forever; this is something we may allow the runtime to have knowledge of sometime in the future. – Josh Watzman Apr 13 '14 at 21:41