Questions tagged [hacklang]

Hack is a open source programming language that reconciles the fast development cycle of PHP with the discipline provided by static typing, while adding many features commonly found in other modern programming languages.

What is Hack?

Find complete documentation at http://www.hacklang.org/

Hack is a open source programming language for HHVM that interoperates seamlessly with PHP, it was invented by Facebook. Hack reconciles the fast development cycle of PHP with the discipline provided by static typing, while adding many features commonly found in other modern programming languages.

Hack provides instantaneous type checking via a local server that watches the filesystem. It typically runs in less than 200 milliseconds, making it easy to integrate into your development workflow without introducing a noticeable delay.

The following are some of the important language features of Hack. For more information, see the full documentation, or follow through the quick interactive tutorial.

  • Type Annotations allow for PHP code to be explicitly typed on parameters, class member variables and return values:

    <?hh
    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):

    <?hh
    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 operand 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:

    <?hh
    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 Shapes, Type Aliasing, Async support, and much more.

176 questions
1
vote
1 answer

Hacklang - Why are protected members invariant?

The public member case With the carte blanche access the calling scope has to them, it's no surprise that public members are invariant: { public function __construct( public T $v ) {} } class ViolateType { …
concat
  • 3,107
  • 16
  • 30
1
vote
2 answers

Hacklang — Why can't I make a nullable cyclic typedef?

I'm trying to implement a recursive container-like structure, and I can understand why a vanilla cyclic typedef would be impossible to realize, but why is the following disallowed as well? typedef cycle = shape('cycle' => ?cycle); // Cyclic typedef…
concat
  • 3,107
  • 16
  • 30
1
vote
1 answer

Are Hack named functions fully first-class citizens?

HHVM 3.9 is not such a fan of ternary statements with named functions, even when passed through fun(), but ≥3.10 is totally fine with them. It seems as though this is one of few cases, however, because 3.9 does accept named functions returned from…
concat
  • 3,107
  • 16
  • 30
1
vote
2 answers

How to successfully blend XHP and ReactJS implementation of a component

Suppose we have the following situation: we have a blog with a posts feed. When the page loads, there should already be like 3 s loaded, created on the server-side; the user will scroll down or press a Load more button and some new post…
Victor
  • 13,914
  • 19
  • 78
  • 147
1
vote
0 answers

Why doesn't Hack infer nullable types?

Any specific reason why Hack doesn't automatically infer nullable types? Instead you have to write ? explicitly. If it used inference instead, wouldn't it be able to check for null for regular PHP programs as well? For example, this function would…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
1
vote
1 answer

Generics and Type casting

Here is very simplified example: function getList():Vector { $values = ['1','2','3','4','5']; $list = Vector{}; foreach ($values as $value) { $list->add((Tx) $value); } return $list; } For instance I know that…
wake-up-neo
  • 814
  • 7
  • 9
1
vote
1 answer

Do Hack programmers use runtime assertions?

In an untyped language, runtime assertions can catch "type errors": With Hack's type annotations, I would want to delete the assertions, but this is unsafe because untyped code…
Ben Greenman
  • 1,945
  • 12
  • 22
1
vote
1 answer

"walk" a PHP array using multithreaded (async) with hack (HHVM)

Not sure why there isn't yet a "hack" tag (sorry to list in PHP), but... I am wondering if it/how it would be possible to walk an array using multiple threads using the multithreaded/async feature of hack. I don't really need this, but it is a…
user1122069
  • 1,767
  • 1
  • 24
  • 52
1
vote
1 answer

Does Travis CI support Hack?

I've written code completely in Hack, and I would like to use Travis CI to test my builds on various HHVM versions with Hack enabled. Does Travis CI support Hack when I select HHVM as the testing platform or is it just PHP?
Rick Mac Gillis
  • 754
  • 8
  • 16
1
vote
1 answer

Push rejected, failed to compile PHP (HHVM) app

I need to develop a web app using HHVM and Hacklang on Heroku. The problem is when I git push to Heroku master, I got this error message: remote: remote: gzip: stdin: not in gzip format remote: tar: Child returned status 1 remote: tar: Error is not…
Nurdin
  • 23,382
  • 43
  • 130
  • 308
1
vote
1 answer

"Internal compiler" error when installing HHVM on Centos 7

I need to install HHVM on my Linux server to enable my hacklang working. But I got this error message during installation c++: internal compiler error: Killed (program cc1plus) Please submit a full bug report, with preprocessed source if…
Nurdin
  • 23,382
  • 43
  • 130
  • 308
1
vote
1 answer

Stack overflow using hhvm for benchmarkgame adapted to strict Hack

Since Hack is faster in strict mode, I tried to translate the first benchmarkgame to dito, but get stuck on a stack overflow that never occur in the un-strict version (different with recursive calls using objects?). Any reasons why? How could I…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
1
vote
1 answer

Did HHVM works only with HACK?

HHVM is a new run time for PHP. I had installed hhvm on Ubuntu. But I got a doubt that can I write php code in .hhvm file. Or I have to install hack and write code as shown below …
Manohar Gunturu
  • 676
  • 1
  • 10
  • 23
1
vote
1 answer

How to Implement Singleton Pattern without Nullable

I am trying to implement the Singleton pattern in Hack. However, I keep running into issues with Nullable.
joshwbrick
  • 5,882
  • 9
  • 48
  • 72
1
vote
1 answer

Hack (PHP) ReflectionClass fails with Map as default property of method

If the signature of a method has a Map collection as the default value of a parameter, an error is thrown when using the ReflectionClass to inspect that method. Cannot use collection initialization in non-collection class The class looks like:
kyleferg
  • 53
  • 6