Using generics in Hack
Hack has introduced some new collection objects that will be a replacement for the array(). But what if you feel that something is missing? What if you have a problem that is easiest solved with another type of object? This is where we use generics.
Say that you want to build a chess game. You may find it useful to have a class representing the board.
This will probably work well for you. But when your manager ask you to build a Tic-tac-toe you have to rebuild your board like to be a Matrix[int][int]=Mark.
A better solution here is to use generics. You use generics as placeholders for types that are unknown at the time of writing. It is still statically typed because the types are defined when you are using an object of that class. Below you see an example of the matrix with generics.
It is important to know the difference between generic and variable. A generic could be any value but can not change after it got its type. See this example with Vector<T>
$x is of type Vector<int> and $y is of type Vector<string>. $x and $y are not of the same type.
It is good practice to name generic types with capital T in the beginning. You may use as many generics you want. Generic types can be used on both classes and functions.
Not supported features of generics
One documented unsupported of generics is to create a new instance of a generic type. This does not work.
Conclusion
Generics are great when you want to create a wrapper object for something or when you are making your own data types. I am about to write a blog post about how the type checker works and how it works with generics. Stay updated.
Leave a Comment