Saturday, August 30, 2014

Hack has introduced many new features that are used for different purposes, such as Error Prevention, Code Reuse, Performance Optimization etc. Following are some of the important language features of Hack:

Type Annotations

Type Annotations allow for PHP code to be explicitly typed on parameters, class member variables and return values. They are like type declarations, except they are optional.

class MyClass {
    const int MyConst = 0;
    private string $x = '';
    public function increment(int $x): int {
        $y = $x + 1;
        return $y;
    }
}

Nullable Types

Nullable types is an extension of type annotations used to specify that a value maybe of a certain type or NULL. They are supported by Hack through use of a question mark 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. The operator can be used on any class or type.

function check_not_null(?int $x): int {
    if ($x === null) {
        return -1;
    } else {
        return $x;
    }
}

Collections

Collections enhance the experience of working with PHP arrays, by providing first class, built-in parameterized types such as Vector, Map, Set and Pair. Collections allow to detect attempts to set values to incorrect types due to application bugs.

/*  Like all objects, collections has "reference-like"
    semantics for assignment, parameter passing, and
    foreach. */

function foo($v) {
    $v[1] = 7;
}

function main() {
    $v1 = Vector {1, 2, 3};
    $v2 = $v1;
    foo($v2);
    var_dump($v1);
    echo "\n";
    foreach ($v1 as $key => $value) {
        echo $key . " => " . $value . "\n";
        if ($key == 0) {
            $v1[2] = 9;
        }
    }
}

main();

Generics

Generics allow classes and methods to be parameterized in the same way as statically type languages like Java and C#). Generics are basically code templates. This allows to create classes that perform the same operations using the same code on classes of different types (such as integers or floating point numbers etc.)

class Box<T> {
    protected T $data;

    public function __construct(T $data) {
        $this->data = $data;
    }

    public function getData(): T {
        return $this->data;
    }
}

There are other features too which include Shapes, Async support, Type Aliasing and much more.

Hassan Ahmad

Hassan is a web designer, developer who has been in this field for 6 years. He has special expertise in Java, C++, HTML/CSS, PHP, JavaScript and WordPress.

0 comments