Hack: Facebook's new programming language for HHVM

PHP

Facebook Hack Language

Facebook has presented Hack – a new programming language based on the PHP syntax and intended for implementation with HHVM (HipHop Virtual Machine). Applications written on Hack can seamlessly interact and go with an existing code written on PHP, as the HHVM environment supports the implementation of unmodified PHP scripts. The Hack code is distributed under a free PHP license. The realisation of the language has already been tested in commercial operation; Facebook, in particular, has already changed to Hack and translated practically all the code of its PHP projects into Hack. The differences between Hack and PHP come down to the support of static typing and a range of advanced facilities, borrowed from other programming languages. For example, Hack supports facilities such as generalization (generics in the likes of C# and Java), null-values, collections (in addition to the traditional arrays, Vector, Map, Set, and Pair have been added), Lambda-expressions (a PHP Closures analogy without the explicit naming of variables), asynchronous programming mechanisms (function categories of async and await), composite shape-structures and the means for redefining label names (type aliasing). The indication of labels takes place via a system of annotations; for example, «function increment(int $x): int {» instead of «function increment($x) {».

From the PHP facilities, whose facilities are not supported in Hack, certain operators like goto, break N, continue N, global, «if:…endif», AND, OR, XOR (must use “&, |, ^“), links («function foo(&$x)”), the symbol of suppression of error messages “@”, the usage of “if/then/else” without framing the block with the symbols “{}”, elseif (the proper way is “else if”), the means of dynamic invocation (eval, $$x and so on), variables within variables ($a = „hello“; $$a = „world“; echo „$a + $hello“), the assignment of a function name through a variable ($func = ‘myFunc’; $func(1,2);), ArrayAccess objects. It can be emphasized that the invocation of function names in Hack is subject to the symbol case, whilst it was not in PHP.

Hack was realized at the level of HHVM, which supports JIT compilation. HHVM fully supports PHP 5 but when programs are implemented in Hack, a significantly higher productivity is provided as the information on types in JIT compilation allows the generation of the most optimum and compact machine code. A seamless support of PHP in HHVM significantly simplifies the porting into Hack; a project can be translated into static types step-by-step, mixing the code and combining the ».php” и “.hh” files (the code can be a mix of static and dynamic typing; for example, one function may have explicitly defined types, and another function may consist of dynamic definitions of types).

Besides, there are tools for the automatic translation of PHP code into Hack. The means of instant type checking via a special local server (hh_server), which tracks changes in the file system and reports data on likely errors, are provided. The usage of the server process reduces the time required for the periodic verification of the code, eliminating the need to wait for the completion of the code analysis stage. Instead, the code is checked as it changes and the result is displayed almost instantly after launching the test (hh_client), without suspending the development process. The plug-ins for the integration of Hack support, including those that take into account the availability of the verification server, are prepared for Vim and Emacs.

Comments

874

Ropes — Fast Strings

Most of us work with strings one way or another. There’s no way to avoid them — when writing code, you’re doomed to concatinate strings every day, split them into parts and access certain characters by index. We are used to the fact that strings are fixed-length arrays of characters, which leads to certain limitations when working with them. For instance, we cannot quickly concatenate two strings. To do this, we will at first need to allocate the required amount of memory, and then copy there the data from the concatenated strings.