Hack (programming language)

From Bauman National Library
This page was last modified on 8 June 2016, at 20:27.
Hack
Hack logo, featuring white lowercase "hack" letters on a black background, with stylized triangular geometric shapes on the left
Designed by Julien Verlaguet, Alok Menghrajani, Drew Paroski, and others[1]
Developer Facebook
First appeared 2014
Typing discipline static, dynamic, weak
OS Cross-platform
License BSD License[2]
Website hacklang.org
Influenced by
PHP, OCaml, Java, C#, Scala

Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Facebook as a dialect of PHP. The language implementation is open source, licensed under the BSD License.

History

Hack was introduced on March 20, 2014. Before the announcement of the new programming language, Facebook had already implemented the code and "battle tested" it on a large portion of its web site.

Features

Hack interoperates seamlessly with PHP, which is a widely used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. A majority of the valid PHP scripts is also valid in Hack; however, numerous less frequently used PHP features and language constructs are not supported in Hack.

Hack extends the type hinting available in PHP 5 through the introduction of static typing, by adding new type hints (for example, for scalar types such as integer or string), as well as by extending the use of type hints (for example, for class properties or function return values). However, types of local variables cannot be specified. Since Hack uses a gradual typing system, in the default mode, type annotations are not mandatory even in places they cannot be inferred; the type system will assume the author is correct and admit the code. However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.

Hack also includes following features: asynchronous programming, aliases, nullable types, collections, lambda expressions and tuples as well as typechecker.

Syntax

The basic file structure of a Hack script is similar to a PHP script with a few changes. A Hack file starts with <?hh tag, as in:

    <?hh
    echo 'Hello World';

An important point to note is that unlike PHP, Hack and HTML code do not mix: either XHP or a template engine need to be used.

Functions

Hack allows types to be specified for function arguments and function return values. Functions in Hack are thus annotated with types like the following:

    <?hh
    // Hack functions are annotated with types.
    function negate(bool $x): bool {
        return !$x;
    }

Classes

In Hack class is specified like the following:

    <?hh
    class MyClass {
      public function alpha(): int {
        return 1;
      }

      public function beta(): string {
        return 'hi test';
      }
    }

Types

The following table summarizes the type system available in Hack, examples of how they are used in annotations, where they can be annotated (function, class method, class property, function/method return, parameter), and if they can be nullable.

Type Example Functions Class method Class property Function Return Parameter Nullable

Primitives


Num


Arraykey


void


noreturn


Objects


Mixed


this


XHP


Nullable


Generics


Enum


Callable


Tuple


Type Alias


Формы

int, array


num


arraykey


void


noreturn


Foo, IBar


mixed


this


XHPRoot, XHPChild


?int, ?Vector<string>


Box<T>


MyEnum


(function(int, string):

string)


(int, string)


MyAlias


MyShape

Yes


Yes


Yes


Yes


Yes


Yes


Yes


No


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes

Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes

Yes


Yes


Yes


No


No


Yes


Yes


No


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes

Yes


Yes


Yes


No


No


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes

Yes


Yes


Yes


No


No


Yes


Yes


No


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes

Yes


Yes


Yes


No


No


Yes


No


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes


Yes

Typecheker

The Hack typechecker is the primary tool that makes Hack such a unique language. Before run time, the typechecker analyzes all the code associated with your program for various typing errors, thus preventing nasty bugs that may only have been exposed at run time. The typechecker statically analyzes your program, catching problems in all paths of your code. This is not compilation. It is super fast feedback on the various states that might occur in your program so you can handle them before your code is run. The typechecker literally monitors changes to your code and updates its analysis accordingly, in real time.

Without the typechecker, this simple example would fail at runtime:

  <?php
  namespace Hack\UserDocumentation\TypeChecker\Intro\Examples\RuntimeFail;
  class A {
    public function foo() { return 2; }
  }
  function failing($x) {
    $a = new A();
    if ($x === 4) {
      $a = null;
    }
    // $a being null would only be caught at runtime
    // Fatal error: Uncaught exception 'BadMethodCallException' with message
    // 'Call to a member function foo() on a non-object (NULL)'
    $a->foo();
  }
  failing(4);

Output:

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function foo() on a non-object (null)' in /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/runtime-fail.php:17
Stack trace:
#0 /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/runtime-fail.php(20): Hack\UserDocumentation \TypeChecker\Intro\Examples\RuntimeFail\failing()
#1 {main}

However, when using the typechecker before you run your code, you can catch errors before deployment, saving users from nasty fatals that may occur.

  <?hh
  namespace Hack\UserDocumentation\TypeChecker\Intro\Examples\TypecheckerCatch;
  class A {
    public function foo() { return 2; }
  }
  function failing($x) {
    $a = new A();
    if ($x === 4) {
      $a = null;
    }
    // $a being null would only be caught BEFORE runtime
    //   typechecker-catch.php:21:7,9: You are trying to access the member foo
    //                                 but this object can be null. (Typing[4064])
    //   typechecker-catch.php:12:10,13: This is what makes me believe it can be
    //                                   null
    //
    $a->foo();
  }
  failing(4);

Output:

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function foo() on a non-object (null)' in  /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/typechecker-catch.php.type-errors:20
Stack trace:
#0 /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/typechecker-catch.php.type-errors(23):  Hack\UserDocumentation\TypeChecker\Intro\Examples\TypecheckerCatch\failing()
#1 {main}

Links

  1. Documentation
  2. Main Page
  3. Github
  4. Tutorial
  5. Install
    1. "Where Credit Belongs for Hack". Bryan O'Sullivan. 2014-03-28. Retrieved 2015-02-02. 
    2. "facebook/hhvm: hhvm / hphp / hack / LICENSE". github.com. Facebook. 2014-03-20. Retrieved 2014-08-11.