A Humbly Arrogant Blog

Posts about software development

Hack: Types

Facebook’s Hack bring lots of features that a lot of other programming languages get to take advantage of. One of major advantages of Hack over PHP is a typing system. So here’s a quick run over of the typing system as I understand it.

Annotate

Annotating is when you define which type is going to be used. You can type the following:

For function arguments you just put the type before the variable name for the argument.

For the return type you just put : type in between the function definition and the function body.

For constants you just put the type in between the const keyword and the constant name.

For class variables you just put the type in between the scope keyword and the variable name.

List of basic types

Primitive are one most basic types that are used in most programming languages.

Primitive Unions are types that can be used to describe other Primitive types that go together. * num - integer or float * arraykey - string or integer

Arrays while being a primitive type can be expanded to show what type of data they hold. You can also define the array key type. Instead of typing arrays like this you may want to look at using collections.

Classes and interfaces, if a class or interface exists in the code that’s being executed then it’s a type that you can use for type annotations.

Other basic types

A full list can be found in hack annotations docs.

Generics

Sometimes you want your code to work with any type. In those cases we can use type generics. Generics can be defined on a class or on a method. To define a generic you wrap the name of the generic with less than and greater than in either the class definition or the method definition.

When naming it you should remember can’t use the name of a type that already exists. It’s a general rule of thumb that you start your generic type with T and for real simple use cases T alone is fine. With collections it’s advised to use Tk for the key and Tv for the value.

Overriding return type signature

When you extend a class you are able to override the return type for methods you override. However the new type must be compatible with the original type. So in other word the type must be a child type of the original type.

So say you originally typed it Foo you couldn’t then change the type to bool. As that’s not a compatible type of Foo. However if you created a child class of Foo called Fooable you would be able to use that as a type. So your code still actually follows the orignal return signature it’s just more detailed.