New Facebook 'Hack' Programming Language Aims To Improve PHP
- By David Ramel
- March 24, 2014
Facebook last week open sourced an internal programming language called Hack, developed by its engineers to improve upon -- while remaining compatible with -- the PHP scripting language running much of the company's Web site.
The new language was developed for use with the internal Facebook HipHop Virtual Machine (HHVM) platform, which helps the company's Web site serve more than 1 billion users. HVVM is described as a complete PHP toolchain -- including an interpreter, a Just-in-Time (JIT) compiler and a debugger -- developed by Facebook to vastly increase PHP performance. It was itself open sourced in 2011.
The main improvement upon PHP offered by Hack, according to Facebook developers, is the capability to use static typing in addition to PHP dynamic typing. This approach is referred to as gradual typing, where variables such as strings and integers can be typed statically at compile time or dynamically at run time.
Dynamic typing typically allows for quick iterations of code, with quick feedback loops that let developers see the immediate impact of their code changes. However, because variables aren't typed until runtime, related errors aren't discovered early in the coding process -- they must wait until a type checking tool analyzes all the code. In contrast, static typing lets developers feel more confident about possible typing problems, but often requires longer development cycles.
"Thus, Hack was born," said Facebook in its Engineering Blog. "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."
Beyond variable typing, other improvements to PHP include the addition of generics, nullable types, shapes, type aliasing, constraints on type parameters, collections, lambdas, asynchronous programming support and runtime enforcement of return and parameter types, and more. Meanwhile, because of incompatibility with static typing, the engineering team decided to leave out some PHP features, such as the extract function used to import variables from an array.
HVVM is still a PHP runtime and Facebook said it plans to maintain that status and eventually reach 100 percent compatibility with PHP 5 sourcecode. The major change is that it now supports both PHP and Hack sourcecode. Most existing PHP files are also valid Hack files, the company said.
Facebook's huge investment in PHP dictated a PHP-compatible solution to address its internal coding issues, but that decision was met with some skepticism on programming-related social sites such as Hacker News and Slashdot.
"I am baffled as to why you'd build your castle atop a crumbling foundation," said one Hacker News reader who was among many to point out PHP's deficiencies on a thread apparently started by Bryan O'Sullivan, leader of the Hack team. "We wanted to preserve our investment in PHP, while introducing new capabilities and safety that make rapid development at scale less daunting," O'Sullivan said in one thread.
Written mostly in OCaml, Hack runs on Mac OS X and Linux, with Windows support in the works and expected this year. The engineering team also added support for the Heroku cloud platform right after last week's announcement.
O'Sullivan noted on the Hacker News thread that developers can now use Hack with the Emacs and Vim IDEs, and he seemed to indicate other options were coming soon: "Stay tuned. You'll hear more in due course," he said.
Another Hacker News reader applauded the addition of async/await features, similar to the Microsoft .NET Framework implementation, and asked if that framework was an inspiration.
"Yep," O'Sullivan replied. "We're happy to be inspired by good ideas when they're obviously the right path to walk."
O'Sullivan noted that Hack is in full production use at Facebook. "100 percent of our Web front-end developers use Hack now," he said on Hacker News. "This has been an organic process of growth over the past year, by which I mean our engineers are using it because they like it and see value in it, not because there's someone standing over them with a big stick :-)."
Facebook is holding a Developer Day to introduce the language next month at its campus in Menlo Park, Calif. Meanwhile, for more on Hack, see the documentation, a getting started tutorial and the GitHub HVVM project page. Facebook developers were also active in answering questions and providing help today on IRC.
David Ramel is an editor and writer for Converge360.