Skip to content

Instantly share code, notes, and snippets.

@ryangreenberg
Created May 19, 2021 12:18
Show Gist options
  • Save ryangreenberg/d9d9c771a9ec000eb6ba2e169d895725 to your computer and use it in GitHub Desktop.
Save ryangreenberg/d9d9c771a9ec000eb6ba2e169d895725 to your computer and use it in GitHub Desktop.
use namespace HH\Lib\C;
<<__EntryPoint>>
function example(): void {
takes_varray(varray[""]); // works for non-empty varray
takes_varray(varray[]); // runtime exception for empty varray
}
function takes_varray(?varray<string> $v): void {
// This truthy check does not pass for an empty varray, but the typechecker doesn't
// track the fact that empty varrays miss this branch and are not converted to vecs
//
// If you replace the if ($v) with the next line, the typechecker can see the problem
// if ($v is nonnull && !C\is_empty($v)) {
if ($v) {
$v = vec($v);
}
takes_vec($v);
}
function takes_vec(?vec<string> $v): void {}
$ hhvm vec_varray.hack
Fatal error: Uncaught TypeError: Argument 1 to takes_vec() must be of type ?HH\vec, array given in .../vec_varray.hack:21
Stack trace:
#0 .../vec_varray.hack(18): takes_vec()
#1 .../vec_varray.hack(6): takes_varray()
#2 (): example()
#3 {main}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment