Yesterday, Facebook officially announced on it’s blog the Hack Language. Hack Lang is a programming language that Facebook deployed for HHVM, a programming language that interoperates seamlessly with PHP.
The main feature of the Hack Language is the fast development of PHP with the discipline provided by static typing. Also, the Facebook team added more features found in modern programming languages.
What is HHVM ?
HHVM comes from Hip Hop Virtual Machine. HHVM is an open-source virtual machine designed by Facebook for executing programs written in Hack and PHP. HHVM uses a just-in-time (JIT) compilation approach to achieve superior performance while maintaining the development flexibility that PHP provides.
You can find more details regarding HHVM here.
Whai is Hack Lang ? How does it work ?
Hack Language has deep roots in PHP. In fact, according to Facebook, the most PHP files are already valid Hack files. Hack is written primarily in OCaml ( Known as Objective Caml, OCaml’s static type system can help eliminate problems at runtime. OCaml is perhaps most distinguished from other languages with origins in academia by its emphasis on performance ). Because of this, the server keeps all information about the source code in memory and automatically updates itself when a file changes on disk. This approach has paid off: the type checker typically runs in less than 200 milliseconds and rarely takes more than a second. So, for a website so big like Facebook, the speed and the stability is very important.
Our principal addition is static typing. We have developed a system to annotate function signatures and class members with type information; our type checking algorithm (the “type checker”) infers the rest. Type checking is incremental, such that even within a single file some code can be converted to Hack while the rest remains dynamically typed. Technically speaking, Hack is a “gradually typed*”* language: dynamically typed code interoperates seamlessly with statically typed code.
Within Hack’s type system, we have introduced several features such as generics, nullable types, type aliasing, and constraints on type parameters. These new language features are unobtrusive, so the code you write with Hack will still look and feel like the dynamic language to which PHP programmers are accustomed.
However, Hack adds additional features beyond static type checking, including Collections, lambda expressions, and run-time enforcement of return types and parameter types.
Source – https://code.facebook.com/posts/264544830379293/hack-a-new-programming-language-for-hhvm/
Examples of Hack Lang Source Code:
1. – Type Annotations allow for PHP code to be explicitly typed on parameters, class member variables and return values:
2. - Generics allow classes and methods to be parameterized (i.e., a type associated when a class is instantiated or a method is called) in the same vein as statically type languages like C# and Java):
{
protected T $data;
public function __construct(T $data) {
$this->data = $data;
}
public function getData(): T {
return $this->data;
}
}
For more examples, go to the official Hack Language page - http://hacklang.org/