features of Hack Language

April 13, 2014

  • Type Annotations allow for PHP code to be explicitly typed on parameters, class member variables and return values:
    class MyClass {
      const int MyConst = 0;
      private string $x = '';
      public function increment(int $x): int {
        $y = $x + 1;
        return $y;
  • 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):
    class Box<T> {
      protected T $data;
      public function __construct(T $data) {
        $this->data = $data;
      public function getData(): T {
        return $this->data;
  • Nullable Types are supported by Hack through use of the ? operator. This introduces a safer way to deal with nulls and is very useful for primitive types that don’t generally allow null as one of their values, such as bool and int (using ?bool and ?int respectively). The operator can be used on any type or class.
  • Collections enhance the experience of working with PHP arrays, by providing first class, built-in parameterized types such as Vector (an ordered, index-based list), Map (an ordered dictionary), Set (a list of unique values), and Pair (an index-based collection of exactly two elements).
  • Lambdas offer similar functionality to PHP closures, but they capture variables from the enclosing function body implicitly and are less verbose:
    function foo(): (function(string): string) {
      $x = 'bar';
      return $y ==> $x . $y;
    function test(): void {
      $fn = foo();
      echo $fn('baz'); // barbaz
Other significant features of Hack include ShapesType AliasingAsync support, and much more.

1 komen:

Blogger said...

If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (no matter why you broke up) you must watch this video
right away...

(VIDEO) Have your ex CRAWLING back to you...?



Popular Posts