#TIL About the Hack Language

Yesterday Facebook open sourced one of the programming languages they’ve been using internally for awhile now, “Hack”, which runs on another open source thing they released awhile ago, HHVM – a virtual machine for PHP and Hack code. Their goal is to “reconciles the fast development cycle of PHP with the discipline provided by static typing, while adding many features commonly found in other modern programming languages”, and considering most of their existing PHP codebase has been ported over to Hack I’d say they’ve done a good job.

Ask any web developer from the early 2000’s and they’ll probably give you a pretty bad review of PHP. While PHP remains a common server-side web development language, its script-based nature along with the ability to be interwoven in HTML resulted in some ugly spaghetti code. At the same time, those two features empowered web developers to hit the ground running; you could write some PHP, save the file, and hit refresh in your browser to immediately see a result.

On the other side of the fence, statically typed languages paired with compilers give developers an extra level of confidence when it comes to creating applications. Using a language that can define rules for the way variables, functions, and object interact allow for a multitude of safeguards at compile time, as well as a number of features that can improve the developer experience. Types, for example, allow us to define exactly what can be passed into a method, meaning we can validate our method calls at compile time instead of at runtime. This reduces the need for extra validation code, and even more important, intercepts invalid method calls before they happen in a production environment.

As you can imagine, the combination of these two paradigms is pretty powerful – a language that you can develop with quickly, while at the same time reduces the amount of errors and manual validation required. That’s what Hack promises to be to PHP developers. In addition, Hack provides some language features that many other languages have provided for awhile – generics, nullable types, collections, lambdas, shapes (aka structs), asynchronous support, and more.

What if you already have a large PHP codebase? Hack promises to interoperate seamlessly with PHP, and also provides a utility to port your existing codebase over to Hack. While not every feature in PHP 5 exists in Hack yet, they do plan to reach full parity with the existing PHP language.