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
2 answers

HHVM staticly typing lookup tables and keeping them fully cached in RAM

I'm doing scientific research, processing through millions of combinations of multi-megabyte arrays. For you to be capable of answering this question you will need to have knowledge/experience of all of the following how HHVM is able to cache data…
user1862165
  • 119
  • 1
  • 8
1
vote
1 answer

Query string parameters are missing

This is a really weird issue which I don't know where to find the solution. Doing a POST request I could get the request parameters from "file_get_contents('php://input')". Also the $_SERVER variable is not empty. It contains data. But $_GET is…
musse1
  • 379
  • 4
  • 17
1
vote
1 answer

How Hack compares to c++?

If hack code is compiled into c++, is the time efficiency of a hack program in the same league as c++, in bash processing algorithm? For example : big loops with float add and mult )
Xmanoux
  • 3,435
  • 4
  • 15
  • 16
1
vote
0 answers

Color coding for HackLang PHP Code in Netbeans

When I write code in Netbeans PHP Project that begins with
Software Guy
  • 3,190
  • 4
  • 21
  • 21
1
vote
1 answer

HHVM non-deterministic behaviour of the typechecker

I've noticed that calling hh_client is not always returning correct result. For example: I have following pieces of code: backend\ConvertMessage.hh:
Luigi
  • 83
  • 5
1
vote
1 answer

Add Elements to nullable Vector

Alright, so I got a private ?Vector $lines which is empty when constructing the object and now I want to add strings to that Vector. The following Hack code works well: $lines; public function…
jeyemgfx
  • 67
  • 8
1
vote
1 answer

Running hh_client on one file hangs forever

I used the build instructions from this link centos install docs It seemed to install fine. I ran hhvm --version HipHop VM 3.5.0-dev+2014.12.11 (rel) Compiler: heads/master-0-g546087bf1b0560c4a9e254fcad46a9212e42ccc2 Repo schema:…
user2108258
  • 803
  • 3
  • 13
  • 20
1
vote
2 answers

Change the value of a lexically scoped variable in a HHVM/Hack lambda expression?

Is it possible to change the value of a lexically scoped variable in a Hack lambda expression? function allTrue(Map $map): bool { $valid = 1; $map->map($a ==> $valid &= $a); return $valid === 1; } $map = Map{'foo' => true,…
robbmj
  • 16,085
  • 8
  • 38
  • 63
1
vote
1 answer

What is "Transformable" in Hack language?

This Facebook example shows, how Hack transpiler removes Transformable type hint from both transform() and wonderland() functions. Why? Is Transformable some built-in Hack interface or class?
Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
1
vote
1 answer

Issues with configuring a simple hhvm site (hack-site-example) with ngnix on ubuntu14.04

I followed the instructions here at https://github.com/hhvm/hack-example-site and somehow lost my way when setting up hhvm hack site on ngnix over ubunut 14.04 . Please note that I used the appropriate apt-get repo for 14.04. However after…
Anand
  • 355
  • 1
  • 2
  • 12
1
vote
1 answer

Setting EnableHipHopSyntax to True with HHVM

When I run my code, I get the following error: Syntax only allowed with -v Eval.EnableHipHopSyntax=true in /var/web/site/myfile.php on line 26 myfile.php has a function at that line that has: public static function set ( string $theme …
user280209
  • 675
  • 5
  • 5
1
vote
1 answer

Why is generic instantiation syntax disallowed in Hack?

From the docs: Note: HHVM allows syntax such as $x = Vector{5,10};, but Hack disallows the syntax in this situation, instead opting to infer it. Is there a specific reason for this? Isn't this a violation of the fail-fast rule? There are…
Maciej Sz
  • 11,151
  • 7
  • 40
  • 56
1
vote
4 answers

Simple code - not outputting message

I'm using Hack, which can be found at hacklang.org. Why doesn't the following code : title
jay_t55
  • 11,362
  • 28
  • 103
  • 174
1
vote
1 answer

Debian 7 Nginx HHVM server not running hh code

I have an nginx Debian 7 server I have configured for hhvm successfully before (with hh code working fine) but this week I did it again (from a fresh install of Debian 7) and it installed successfully and even responds "HipHop" when i request the…
0x01
  • 15
  • 4
1
vote
2 answers

Why is it easier to refactor a typed language?

I was going through the documentation of Facebook's Hack language and it said one of the advantages is refactorability. Why is Hack more easily refactorable than PHP just because it is partially typed?
user1210233
  • 2,730
  • 5
  • 24
  • 31