I am trying to implement the Singleton pattern in Hack. However, I keep running into issues with Nullable.
<?hh //strict
class Foo {
private static Foo $foo;
public function __construct() {
// Do stuff here.
}
public static function theFoo(): Foo {
if (null === self::$foo) {
self::$foo = new Foo();
}
return self::$foo;
}
}
$aFoo = Foo::theFoo();
When executed I get the error:
Catchable fatal error: Hack type error: Please assign a value at foo.hh line 4
The type checker returns a similar as well:
foo.hh:4:24,27: Please assign a value (Typing[4055])
How do I assign a default value to a static property?