Pages

Tuesday, March 25, 2014

The Hack Language

Table of Contents 

  • What is Hack?
  • Hack Background
  • Summary
Hack is a language for HHVM that interoperates seamlessly with PHP. HHVM supports both Hack and PHP; code can be run on HHVM in either language or even a mixture of both.
To get started with Hack, it really is as simple as:
  • Use <?hh at the top of your file; you can also change <?php to <?hh in existing PHP files, and your code will run just as before in HHVM.
  • Optionally name your file with the .hh extension to distinguish your Hack files from your PHP files. Of course, you can keep the name of the file .php (or any other extension that you use)
  • Optionally start using Hack features

    What is Hack? 

    Hack is a language for HHVM that interopates seamlessly with PHP. The barrier to entry for Hack is low. To get started, all that is needed is to generally understand the features that Hack provides and how to call the Hack type checker(hh_client invokes the type checker at the command line)
    The goal of Hack to offer developers a way to write cleaner, safer and refactorable code while trying to maintain a level of compatibility with current PHP codebases. The primary way that this goal is achieved is to provide developers a way to annotate PHP functions and classes with type information, providing a type checking tool to validate those annotations. In addition, Hack also provides specific language feature additions such as:
Each of the Hack features, and more, is discussed in great detail in this documentation.
Hack code is indicated through the <?hh marker at the top of a file, as opposed to the canonical <?php marker.
Very important note: The type checker is invoked before runtime to determine whether code is typesafe. While HHVM knows about and understands the Hack language, it does not necessarily strictly enforce them. This is different from a true statically-typed language like C# where code is type checked at compile time and enforced (usually via some sort of exception mechanism) at runtime.

Hack Background 

Facebook was initially built with PHP. Parameters and return types were specified in comments. Parsers could use this to create documentation. But type-checking was non existent.
<?php/**
 * Wraps a name in a box
 *
 * @param raw-str $name
 * @return html-str
 */
function say_hello($name) {
  return 
"<div class=\"box\">Hello ".htmlize($name)."</div>";
}
In 2009, a PHP compiler called HipHop was released. In 2010, minor changes to the PHP language were introduced by the HPHP team to improve development time and provide basic type safety. The changes were XHP and parameter type constraints.
<?php/**
 * Wraps a name in a box
 *
 * @return xhp
 */
function say_hello(string $name) {
  return <
div class="box">Hello {$name}</div>;
}
In 2012, Facebook engineering teams started exploring the idea of annotating return types. And the Hack language was born...

Summary 

Hack provides the following, non-exhaustive list of features:
  • Ability to annotate function and method return types.
  • Ability to annotate the type of member variables.
  • Protection against common unsafe coding patterns (e.g. sketchy null checks).
  • Type-safe collections (Vector, Map, Set).
  • A tool to check the types (the Hack type checker).
Hack has been designed with PHP compatibility in mind. The type checker enforces checks on typed code and assumes calls to/from PHP and other untyped code are correct. This means the amount of type checking will gradually increase as more and more code is written in Hack.
Hack is unlike many other statically typed languages:
  • The type checking phase is instant.
  • Local types are inferred.
  • There is a minimal need for casting.
  • The type related error messages are designed to make it easy to fix the core issue.
  • The type checking model is a best effort model. Hack code can call PHP code and vice versa.
Hack comes with the following benefits (and possible benefits):
  • Return types makes the code more readable.
  • Some typing mistakes will be caught by the type checker.
  • Better emacs/vim/etc. integration.
  • Future: Reliable refactoring tools.
  • Future: Most typing mistakes will be caught statically.



No comments:

Post a Comment