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
0
votes
1 answer

Combine multiple generic types in hacklang

I am trying to implement the reduce function from underscore in hack. In underscore, the reduce function has the following behavior: If no memo is passed to the initial invocation of reduce, the iteratee is not invoked on the first element of the…
nwarp
  • 731
  • 4
  • 8
  • 17
0
votes
1 answer

Typechecking Hack code on VirtualBox via NFS shared folder

It seems prudent to first mention this issue and then this aptly-named edit which seems related and has made hh_server refuse to run on NFS file systems. I am not very familar with file systems and have never touched OCaml before, so in trying to…
concat
  • 3,107
  • 16
  • 30
0
votes
2 answers

Hack iterate (map) over Map

I have a Map such as: $m = Map { 'sort' => 'created', 'order' => 'desc', } I want to turn that into a string: 'sort:created order:desc' I can do this with arrays as explained in this SO answer: implode(' ', array_map(($k, $v) ==> $k.':'.$v,…
Martin Konicek
  • 39,126
  • 20
  • 90
  • 98
0
votes
1 answer

Hacklang: Cannot return Vector from (function(...): KeyedIterable)

I have a method that might return either a Map or a Vector, and since both types implement KeyedIterable — Vector specifically implementing KeyedIterable — I figured I could cover both cases with a KeyedIterable return type.…
concat
  • 3,107
  • 16
  • 30
0
votes
1 answer

HHVM is installed, but typechecker is missing from environment according to error.log

I start with the following inside of a file:
OCDev
  • 2,280
  • 3
  • 26
  • 37
0
votes
1 answer

unexpected T_OBJECT_OPERATOR but no TypeChecker errors (Hacklang)

byKey('destination_id')->map($stringed ==> (int) $stringed); $this->ids->addAll($remapped); } foreach ($list as $id) { …
wake-up-neo
  • 814
  • 7
  • 9
0
votes
1 answer

Self/child static construct (hacklang)

Guess it's impossible design for Hacklang?
wake-up-neo
  • 814
  • 7
  • 9
0
votes
1 answer

Catchable fatal error: Hack type error

Example from presentation on page 31 class Foo { public function add(T $delta): Foo { $this->num += $delta; // line 6 return $this; } public function get(): T { return $this->num; } public…
Alexandre Kalendarev
  • 681
  • 2
  • 10
  • 24
0
votes
1 answer

Hacklang Higher order functions for Collections

Do Hacklang Collections have higher order functions such as Reduce, Some, All or an easy way to implement such methods. The Collection I am most focused on is the Vector. It seems to only have Map and Filter. The others would help in writing more…
Mike
  • 386
  • 2
  • 4
  • 16
0
votes
1 answer

Async server in hacklang

I'm trying to create async server in hacklang. File name is first.php: { $master = stream_socket_server("tcp://$host:$port"); …
0
votes
1 answer

How to get inline error checking working with Atom/Nuclide and Hack?

Nuclide supposedly supports error checking, but I can't figure out how to set it up. I have a fresh install of Atom I installed Nuclide via apm install nuclide-installer I've verified Hack is installed correctly by running hh_client from my project…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
0
votes
1 answer

HackLang type for different objects

Let's say I have static connector that allows to proxy instances of different adapters: $m = Connector::take('mcrouter'); $db = Connector::take('production_database'); Connector must init and handle connections during the runtime: protected…
wake-up-neo
  • 814
  • 7
  • 9
0
votes
3 answers

Get children of an XHPChild

I am trying to move my website to Hack and XHP, of course. Below is a structure of what code structure I want to achieve: stories
Victor
  • 13,914
  • 19
  • 78
  • 147
0
votes
1 answer

HHVM Class Undefined SleepWaitHandle

I'm trying to use HHVM's async functions in a larval application. I added the async keyword to my function but I get an error on the line with await SleepWaitHandle. It says that the class is undefined. It doesn't seem like the documentation has…
user2108258
  • 803
  • 3
  • 13
  • 20
0
votes
1 answer

Hacklang - Search Map by value

G'day While in strict mode, is there a way to search a Map via value rather than key? Say I have a Map: $temp = Map{'melon', 'apple'}; How could I search via value?
Tim
  • 176
  • 1
  • 15
1 2 3
11
12