Dynamically typed programming languages build complex websites fast. Statically typed languages make software easier to manage with fewer servers. Facebook is trying to create a language that does both.
Hack is designed to combine the fast development cycle of PHP with the static typing of languages such as Java. The result is a landmark gradually typed language where dynamic and static code interoperate.
After a year of deploying the language and migrating almost its entire PHP code base to Hack, Facebook announced it’s releasing an open-source version to the public.
“Traditionally, dynamically typed languages allow for rapid development but sacrifice the ability to catch errors early and introspect code quickly, particularly on larger codebases,” wrote Facebook software engineers Alok Menghrajani and Julien Verlaguet. “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. Thus, Hack was born. We believe that it offers the best of both dynamically typed and statically typed languages, and that it will be valuable to projects of all sizes.”
Hack was developed for the HipHop Virtual Machine (HHVM), the PHP virtual machine Facebook created in 2013 to face the strain of running the massive amounts of PHP code on an expanding number of servers. Facebook was built in 2003 in PHP, but as millions of users joined the social network, the dynamically typed language began to show its limitations.
HHVM was Facebook’s first step in evolving its codebase for the future of Web development, and Hack is supposed to finish the job.
Aside from gradual typing, some other features Hack incorporates from other languages include:
• Collections: Designed to work with higher-order style functions such as map() and filter(), the Collections API is what Menghrajani and Verlaguet called “a clean, type-safe alternative to PHP arrays.”
• Lambda expressions: One of the hallmark features of the recently released Java 8, the expressions give a concise syntax for creating closures. Hack’s lambda expressions automatically infer variables to save programming time.
• Run-time enforcement: Improves detection and diagnosis of problems within return and parameter types, reducing code errors by optimizing type annotations to help HHVM’s Just-In-Time compilation produce more efficient code.