This article is strongly connected with the one about the HipHop virtual machine, published in the issue nr. 21 of this magazine. When Mark Zuckerberg wrote the first source code lines for Facebook, he had to choose a programming language in which to develop his new social networking site. He chose PHP because, as we all know, it has a very fast feedback loop: you make a change, save the file, press F5 in your browser and you can instantly see the effect. This is possible because the programmer doesn"t have to go through any compilation step. Another reason is that PHP is dynamic: it doesn"t ask for explicit types for the variables within the code.
All this is in contrast with the statically typed languages like Java, C#, C++ etc. In these cases, compilation is a mandatory step and, in the case of big projects, not a fast one.
The good news is that the static model of these languages offers some big advantages: bugs can be detected very early and static information about the variables offer a sort of automated documentation of the code.
Facebook"s front-end engineer team size grew from 80 people in 2008 up to 1000 in 2014. Working with such a big team doesn"t scale very well. Combining this with the fact that, at Facebook, new code is pushed to production twice a day, you end up with a lot of code and a big number of potential problems.
So, the team decided to borrow some of the advantages from the static world to reduce the risks associated with managing such a big team of engineers.
The Hack language was born out of this desire: a hybrid language that connects the dynamic and static worlds.
As I have mentioned above, Hack is a hybrid language which brings static advantages to PHP, which is a dynamic language. It"s nothing else than a PHP syntactic extension.
At the moment, Hack runs only on the HipHop virtual machine version 3.0 and newer. It was made open-source in March 2014.
An important thing to remember is that if you know PHP, then you know Hack.
There are two important features of Hack that ensured its adoption by Facebook"s engineering team and, maybe in the future, in other 3rd-party applications:
Hack comes with some interesting features on top of what PHP is already offering. To see the syntax and more details of what I mention below, please check the language official documentation.
This is by far the most important feature. It allows specifying data types of: function parameters, function return values and class properties. The data types of variables from actual blocks of code cannot be declared; instead, they are inferred by the virtual machine.
A lot of programmers specify this information as comments. Hack formalizes this instead.
The available data types are generally the ones that can be seen in other statically typed languages: primitive types, strings, class names, "void", "this" etc.
This feature allows faster detection of bugs. Also, given that it provides extra static information, the virtual machine"s JIT compiler will be able to better infer what execution paths will be taken. This translates into better performance.
A lot of languages have this feature. In Hack, generics are an even stronger static information provider, on top of the type annotations.
Their syntax and implementation is simpler and more limited than for example in Java or C#. Though, in Hack, you can specify primitive types and even object instances as the generic type.
There are, however, some special cases related to this feature, which must be taken into account when using it.
Most statically typed languages offer such functionality, so it"s welcomed in Hack as well.
The most important classes in this framework are "Map", "Vector" and "Set". Each of them also has an immutable version.
Typically, PHP code relies heavily on the use of arrays. By using these classes instead, code will become clearer and, in some cases, improvements to performance can also be observed due to the specialized use-cases of these classes.
PHP doesn"t support multi-threaded execution. But Hack introduces a primitive implementation of such a feature.
These are similar to the already existing PHP closures, but lambdas eliminate part of the limitations of closures and offer more flexibility. The syntax is similar to the one in Java 8.
This is an optional extension of Hack that allows using XML/HTML blocks as standard syntax blocks. This makes work a lot easier within code that deals with logic and tasks that are specific to templating.
Hack also offers other minor features worthy of attention. Also, part of PHP"s syntax elements that are rarely or incorrectly used are deactivated, such as the "@" operator, the "goto" statement, globals ("global $x") and many more. All this will force the programmer to follow good coding practices.
Below, you can see a piece of code that shows off a lot of the features described above. You can see how type annotations are declared, how a collections class is used, a generics example and a simple lambda expression:
class MyExampleClass {
public int $number;
public CustomClass $myObject;
private string $name;
private array $stuff;
private MyBag $container;
function doStuff(MyBag $container, ?bool $x) : string {
//immutable Map
$map = ImmMap {
‹A› => $container->getA(),
‹B› => $container->getB(),
};
if ($map->get(‹A›) == $container->getA()) {
//error here because $map is immutable
$map->set(‹C›, $container->getC());
}
//$x is of type ?bool, which means:
//a). it›s of type boolean
//b). can also be null
if (! is_null($x) && $x) {
//lambda expression
//and alternative/classic syntax for
//accessing map elements
$result = $r ==> $r + $map[‹A›] + $map[‹B›];
} else {
$result = 0;
}
//error here because function›s return type is string
return $result;
}
}
The HipHop virtual machine includes some very useful Hack tools:
The errors reported by the typechecker are strictly related to type annotations. Its response is very fast due to the constant monitoring of the filesystem.
However, the typechecker does have some limitations:
The Hack language is a breath of fresh air for a language whose development stagnated in the last few years. Its hybridity due to its use of a static model on top of a dynamic language is an interesting concept which we"ll surely see again in the next few years.
Personally, I"m impressed by the elegance in which Hack"s features were implemented and combined with the existing PHP syntax. There are many aspects (especially syntactic ones) which I didn"t cover at all. So I invite you to explore them on your own, hoping that this article has made you curious enough to do so.