lundi 15 juin 2015

How to Implement Singleton Pattern without Nullable

I am trying to implement the Factory 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?

Aucun commentaire:

Enregistrer un commentaire