Hack constructors
Hack got a lot of features and improvements of different kinds. It comes with some syntactic sugar. The constructor is a subject for a syntactic sugar improvement.
The PHP-way of writing a class might look like this:
The “problem” with this is that the spelling error on line 7 might get undetected. The Hack type checker might help you with this if all the supertypes are written in Hack. But in PHP, you might not find this bug until production.
With the new syntactic sugar in Hack you may write the same (without the spelling error) class variable and constructor like this:
Annotations
It is tempting to annotate constructors with void or this. Both make kind of sense since you are not returning anything, but then again you are getting an instance of the class. But how will it work when using this and class inheritance? To avoid all confusion, don’t annotate a return type on the constructor. In fact, the type checker will throw an error if you do annotate the return type of a constructor.
Leave a Comment