The function below is not real, for demo only:
function acceptHackArray(mixed $x):someType {
....
// Need to check is $x is a vector of integers
$tmp = '$x is vector<int>';
...
return something;
}
Take a look at the type-assert library built by HHVM devs, in particular the VectorSpec implementation. At the time of writing, for primitives especially it's nothing too magical, first checking the container type is Traversable, then iterating over the array and asserting the contents into the desired type. You could snip just the relevant bit, using is
on generic types for >= 3.28 (or is_vec
for older versions if you don't mind exactly vec
):
function acceptHackArray(mixed $x): someType {
invariant($x is Traversable<_>, '$x is not Traversable');
$tmp = vec[];
foreach($x as $v) {
invariant($v is int, '$v is not int');
$tmp[] = $v;
}
// $tmp is now vec<int>
// ...
}