I'm learning about the Hack langage and I wanted to create a specific type like this one :
type Points = array<array<int, int>>;
And then I defined that function :
function printPoint(Points $point){
var_dump($point[0], $point[1]);
}
When I pass this array as parameter : [0, 0], there no error emited and the output is :
int(0)
int(0)
And when I pass this one : [[0, 0], [0, 0]], and the output is like it should :
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
My problem here is : why does this work ? (I know it's not the usual question x) ). Why is there not a notice or even a fatal type error in the first test ?
Thanks ;)
At run time, generics are erased. This means that the runtime only knows that Points
is an array, not what type of array.
Instead, you want to use the type checker -- hh_client -- for this. With a file like:
<?hh
type Points = array<array<int, int>>;
function printPoint(Points $point) {
var_dump($point[0], $point[1]);
}
function test(): void {
printPoint([0, 0]);
}
It gives the error you expect:
test.php:10:14,14: Invalid argument (Typing[4110])
test.php:3:21,25: This is an array (used like a hashtable)
test.php:10:15,15: It is incompatible with an int