Issue 26

The Hack Language

Radu Murzea
PHP Developer


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.

Hack in details

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:

  1. Interoperability. With very few exceptions, if you have PHP code, it means you have Hack code. That is because within the same application, you can have PHP code that calls Hack code and the other way around. The two languages can be interchanged because they are represented in the same way at runtime: as HHBC (HipHop Byte Code).
  2. Gradual translation. Hack is developed with a strong aspect of graduality in mind. What this means is that the programmer is free to decide which files in his application will be translated to Hack and, on top of that, how (in)complete that translation should be. Yes, that"s right: you can gradually add Hack features to your code, as many or as fast as you want.


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.

Type annotations

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.

Asyncronous code

PHP doesn"t support multi-threaded execution. But Hack introduces a primitive implementation of such a feature.

Lambda expressions

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:

  1. The ones necessary for translating a PHP codebase into a Hack one. These are relatively easy to use and they automate most of the process, so that a minimal amount of work is left for the programmer. However, on a large codebase, the translation can take a while. For example, to translate Facebook"s 25 million lines of code into Hack, 12 hours of computing time and 10 GB of memory were necessary. In most cases though, it shouldn"t take longer than a few minutes.
  2. The ones that real-time type validations while writing Hack code. This is done by an advanced typechecker that runs as a daemon on the system and is constantly monitoring the filesystem. When there are errors, they are reported in real-time within the text editor with the help of plugins. At the moment, such plugins exist only for vim, emacs and Sublime text editors but the Hack promised to develop equivalent plugins for most popular IDEs and text editors.

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 intended use-case is for it to be used within a project that includes an autoloader. It will work without it, but not optimally
  • all class and function names must be unique within the project to ensure correct results
  • it doesn"t support conditional declarations of functions and classes
  • switching back and forth between Hack and HTML code is not supported because such a programming practice is no longer used and the Hack team are trying to discourage it


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.



  • Accenture
  • BT Code Crafters
  • Accesa
  • Bosch
  • Betfair
  • MHP
  • Connatix
  • BoatyardX
  • .msg systems
  • Yardi
  • Colors in projects