Pages

Saturday, March 29, 2014

Facebook recently announced the release of Hack, a new programming language for the HipHop Virtual Machine (HHVM) aimed at solving some of the underlying issues in PHP. Hack combines dynamic and static typing to create a language that can be deployed fast while maintaining code integrity early on. Facebook has already converted a significant portion of its PHP codebase into Hack.
According to a blog post from Facebook's Julien Verlaguet, a software engineer, and Alok Menghrajani, a security engineer, "Traditionally, dynamically typed languages allow for rapid development but sacrifice the ability to catch errors early and introspect code quickly, particularly on larger codebases. Conversely, statically typed languages provide more of a safety net, but often at the cost of quick iteration. We believed there had to be a sweet spot."
HHVM is an open source virtual machine designed by Facebook to convert PHP into an intermediate code, C++, which is then converted to x64 machine code by a just-in-time (JIT) compiler. This all adds up for a much faster runtime than if the original code was executed on its own. However, PHP is also known to cause some issues when compiling code for the first time and as a language that is difficult to debug.
Hack's addition of static typing to the formula is probably the most important addition to the PHP foundation. Statically typed code can be integrated seamlessly with dynamic code; Facebook calls this a "gradually typed" code. The company also says the new static features are unobtrusive so there will be little difference visible between Hack and PHP you are used to.
Other features present in Hack include:
  • Lambda expressions which allow programmers to save time when working with variables from enclosing scopes.
  • A Collections API that provides high-level functions such as map() and filter().
  • Run-time enforcement of return types and parameter types for easier debugging and more trustworthy type annotation.
Hack was written in OCaml and has an open source version available on GitHub. For more information on Hack visit hacklang.org.