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.
In Hack, Facebook has created a more precise language designed to catch flaws, while maintaining the rapid development cycle that has made PHP one of the most popular languages for Web development.
“This is just the first step, and we are dedicated to continuing to evolve this software to make development even easier for both our own engineers and the broader community,” wrote Verlaguet and Menghrajani. “Hack’s value is not limited to big projects: With type information, good error messages, and fast feedback, small codebases can reap the benefits of Hack as well.”
Facebook is giving a hands-on introduction to the language at a Hack Developer Day on its campus in April.