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

Getting undefined class while using XHP with HHVM although the necessary includes have been made

I'm using nginx 1.6.2, with ubuntu 14.04 and hhvm 3.3.0. My server is configured locally and I'm able to access its pages and execute regular hack scripts. The problem comes when I try to execute scripts that have to include the XHP library. They…
2
votes
1 answer

Hack language: generics for collection types

Why is this code working? $v):void { print_r($v); } test(Vector {1, array("I'm an array"), 3}); Shouldn't it throw an error? What is the supposed to be for?
Cequiel
  • 3,505
  • 6
  • 27
  • 44
2
votes
1 answer

Is Hacklang a stateful or stateless language?

Recently, Facebook released a new language called Hacklang, which is compiled to machine code by HHVM. So I just wonder, is Hacklang a merely stateful language? Thanks.
Tiep Doan
  • 105
  • 6
2
votes
1 answer

Can I use HHVM side by side with the regular PHP interpreter

I'm aware that HHVM can run regular PHP code. What I would like to know is if I can migrate in a way where I serve all files written with hack with the HHVM and all the files written with PHP via the regular PHP interpreter. I assume this should be…
Christoph
  • 26,519
  • 28
  • 95
  • 133
2
votes
1 answer

Hacklang tutorial - this type in extend class

What is correct answer for hack tutorial exercice 16? Link to tutorial: Hacklang tutorial My modified code (not marked as solution):
Atiris
  • 2,613
  • 2
  • 28
  • 42
1
vote
1 answer

How to instantiate a shape type object in hacklang

This seems like it should be in the documentation, but it isn't. I have a shape type. type ThisIsMyShapeType = shape( 'some_prop' => bool, ); How do I instantiate an object of this type ?
kneighbor
  • 23
  • 3
1
vote
1 answer

Reading nested dictionaries in PHP / Hack

I'm trying to have a simple nested dictionary, and then read a nested value $response = dict[ 'some_other_key' => 'asdf', 'sub_response' => dict['success' => false], ]; if ($response['sub_response']['success']){ // do stuff } I am so…
kneighbor
  • 23
  • 3
1
vote
1 answer

Hacklang async function seems to be blocking

I'm learning Hacklang and as a quick test wanted to verify that asynchronous features worked the way I understood them to: that the execution thread would hop over to handle another request while an async operation was in progress. To test this I…
stevendesu
  • 15,753
  • 22
  • 105
  • 182
1
vote
1 answer

Concurrent block in hacklang

Since hack is a single threaded language, what is the benefit of using a concurrent block? concurrent { await func_a; await func_b; } My understanding is that one job is waiting until the other job is over.
Amir
  • 307
  • 3
  • 14
1
vote
1 answer

async function as type in hacklang

I am trying to build a shape where key is string and value is function. This works fine for regular functions but gives error for async functions. Following is what I am trying to do const type TAbc = shape( 'works' => (function (): string), …
Abhishek Jha
  • 935
  • 2
  • 10
  • 22
1
vote
2 answers

How to properly install Hack and HHVM on Windows 7

I am trying to install and configure Facebook programming language Hack and HHVM on Windows, probably Windows 7. I could not find any solution on it. I saw a Bitnami link which suggest HHVM Installers. source I have downloaded the installer files…
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38
1
vote
0 answers

Construct a McRouter instance in Hack

I am new to hacklang and I am experimenting and trying to connect to Memcached which is running on my local machine .I am following the official documentation here where its advised that we use McRouter class in HHVM stdlib to connect to the…
Abhik
  • 1,920
  • 7
  • 27
  • 50
1
vote
1 answer

Inconsistent error while storing a darray into a Shape

I have a shape like this const type TFileInfo = shape( 'displayName' => string, 'givenName' => string, 'jobTitle' => string, 'businessPhones' => vec ); private Person::TFileInfo $person; Now my…
Abhik
  • 1,920
  • 7
  • 27
  • 50
1
vote
0 answers

How to convert from a tuple to a vec or varray in Hacklang?

Tuple doesn't seem to implement Traversable. So, how would one convert a Tuple to a vec in Hack?
Ben H
  • 3,136
  • 3
  • 25
  • 34
1
vote
1 answer

Invariant to restrict function overriding in Hack PHP

I have a base class in PHP Hack with a function: // This method is used to return just one Apple Type protected static function Apple(): AppleType { return AppleType; } Now I have two types of classes - one using a base trait, one doesn't. The…
ADT
  • 11
  • 4