Question

The Hack manual makes it fairly clear how to type-annotate function parameters, function return types, and member variables. However, all of the ways I've tried to type-annotate a global variable or function-local variable result in a syntax error:

string $foo   = "foo";        // unexpected T_VARIABLE
$foo : string = "foo";        // unexpected ':'
string $foo;   $foo = "foo";  // unexpected T_VARIABLE
$foo : string; $foo = "foo";  // unexpected ':'

Are such annotations possible? If it is possible, then what is the correct syntax? If it is not possible, then is this by design or is it something the developers plan to implement? (It certainly would be useful.)

Était-ce utile?

La solution

It's not possible, and this is by design.

Local variables have their types inferred, and global variables don't get type checked (as they can be changed at any time from anywhere by accesing $_GLOBALS).

If there's a particular page of the documentation that could make this clearer, please click the "File A Documentation Bug" link at the bottom of it, so it can be added.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top