Right before the unveiling of Hack, Facebook developers Julien Verlaguet and Alok Menghrajani wrote on the company’s engineering blog. Their words were “Every PHP programmer is aware of the day to day tasks that can be tricky or cumbersome.” So began the rumours that something was cooking within Facebook.

Hack has been projected by the world’s biggest social network as a programming language that is specifically designed to operate seamlessly with PHP. However; just like we mentioned in the previous blog, there is a lot more to Hack than just being PHP’s wing man.

We’ve already established that Hack has improved existing programming languages. Now comes the million dollar question. How?

To answer that, here are some of the key features of Hack that separates it from the other mainstream programming languages in the business.

1. Type Annotations

Preachers of esoteric programming languages such as Haskell will no doubt have a cheeky smile on their face when they read this. The first (and arguably the biggest) innovation that Facebook has introduced through Hack is incorporating features from both dynamic and static typed languages. Static typed languages like C or Java require programmers to explicitly give data type with every variable that they declare. Dynamic typed languages such as PHP do not require a specific data type until the program actually starts running. Even though the dynamic nature of programming languages seems like a far better and convenient choice, it also brings with it some undesirable scenarios. Hack has taken the safer route by using the best of both.

2. Generics

Taking a cue from languages such as C++ and Java, Hack allows classes and methods to be parameterised. In order words, method declarations can have more than one ‘placeholder name’. These are called ‘type parameters ‘ and are associated with types through arguments. Hack has very intelligently utilised this feature to add another, in form of ‘Vector’. We’ll discuss Vector in more detail later but for now, keep this mind: from the perspective of generics, the type parameter T makes Vector generic. Therefore it can hold any type of value, be it a string, an integer or an instance of a class.

3. Nullable Types

Nullable Types might look like a very basic feature to add. But believe it or not, the majority of programming languages do not accept null values when passed to a parameter such as an integer or a Boolean. Many operations, especially aggregations, are affected because of this. With Hack, the ? sign can be used ‘(?int)’ to allow null values and also to safely deal with them.

4. Collections

Hack has introduced its own version of PHP Arrays, promising better performance, readability and correctness. Hack calls its Vectors, Lists and Maps ‘deeply specialised object versions of arrays grouped in a common container’. While the set is most similar to an array in usage, the set, pair and vectors resemble Lists, Associative Arrays and Tuples respectively.

5. Lambdas

Less verbose than the PHP closures, Lambdas help Hack create closures. Lambdas find utility with functions like ‘array_filter()’ or ‘array_map()’. ‘==>’ is used as the lambda operator. Hack expects more readability and shortening in code lines with the use of lambda in the code.For example:

To say that Hack and PHP can work together to deliver faster results than PHP alone isn’t entirely untrue. Through the above features, Hack can break the shackles that bind PHP and unveil a whole new world of possibilities to developers. However, PHP is and will remain an integral part of computer programming. With Hack, PHP can simply make its output faster and more accurate.
It’s still early days so we won’t make a strong claim about how successful Hack can be just yet. But judging on what we’ve seen so far, it certainly looks like a good bet.