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

Hybrid PHP/Hacklang: Use the typechecker on regular PHP with commented type annotations

I can't build hhvm at the moment for lack of access to a 64-bit VM, so I haven't been able to use the typechecker that they have. Their documentation doesn't seem to describe the operation of the typechecker (hh_server and hh_client?) in any…
Calpau
  • 921
  • 10
  • 21
4
votes
1 answer

hh_client reports errors on package

I am following the instruction in Getting started on official Hacklang website. As it says, I run: $ touch .hhconfig $ mkdir bin src tests $ cat > hh_autoload.json { "roots": [ "src/" ], "devRoots": [ "tests/" ], …
Clover Ye
  • 253
  • 3
  • 8
4
votes
1 answer

Most efficient way to print variable number of characters in Hack?

I'm working on a Hack project and I've come across a situation where I need to print $n spaces. Here's how I'm currently doing it: for ($i = 0; $i < $n; $i++) echo " "; I'm wondering whether $n calls to echo is the most efficient way to go about…
Tom
  • 350
  • 4
  • 21
4
votes
2 answers

How to access optional shape field in hack?

To give an example, let's assume I have a type foo of the following shape, type foo = shape( ?'bar' => float, ... ); Now if I try to access value of field bar in the following way, do_something_with($rcvd['bar']); where $rcvd is of foo type,…
Fallen
  • 4,435
  • 2
  • 26
  • 46
4
votes
2 answers

Hack - how to check that an instance uses a Trait?

How can I check if a an instance of a class uses a Trait? I can't use instanceof because the Trait is uninstantiable.
Corey Wu
  • 1,209
  • 1
  • 22
  • 39
4
votes
1 answer

PHPStorm for Hack Language

I am kinda of a new user here and don't have enough reputation points to comment/ask on this question: IDE support for Hack Lang. So, I am hoping that since the last entry provided by Themis Beris someone has been able to get PHPStorm working…
Gabriel
  • 77
  • 7
4
votes
3 answers

Hack typechecker not recognising 'global' keyword inside a function

I'm using HHVM to write a system tool and I cannot for the life of me figure out why this code issues an error when I run hh_client $__al_paths = array(); function requires(string $classPath): void { global $__al_paths; $className =…
Jader Feijo
  • 1,209
  • 1
  • 10
  • 11
4
votes
1 answer

Performance difference between Hack / HHVM vs PHP / HHVM

I recently analysed performance differences of two test PHP scripts by running them on various combinations of Apache / NGinx / HHVM / ReactPHP. My question now is if there is an expected performance difference between a PHP script executed on HHVM…
T-Man
  • 51
  • 5
3
votes
2 answers

Builtin for checking if vec contains specified element

Let's say, I have a vec containing a list of integers that may be non-continuous (due to element being removed from database). Example: $occupiedNumbers = vec[1, 2, 3, 5, 6, 8, 10, 11, 12, 13, 15, 16]; Now what I need is to check if this vec…
Lukasz032
  • 354
  • 4
  • 14
3
votes
1 answer

How to iterate fields of a shape in hacklang?

Say I have a shape like this $something = shape( 'some_key' => ..., 'another_key' => ..., ... ); How can I iterate each field of the shape? I'm looking for something like this foreach ($something as $key) { ... }
kebab-case
  • 1,732
  • 1
  • 13
  • 24
3
votes
2 answers

How to define empty dict in hacklang

I have following shape: const type A = shape( 'b' => dict, ); How do I create this shape with empty dict? For example from this function function getA(): A { return shape( 'b' => ??? ); }
Taras Mazepa
  • 604
  • 8
  • 24
3
votes
0 answers

Confuse how to use Repo Authoritative mode on HHVM using Ubuntu 16.04 LTS

I just tried to build some website using hack lang developed by facebook, and found an interesting feature called Repo Authoritative. I had tried to follow the official docs : https://docs.hhvm.com/hhvm/advanced-usage/repo-authoritative But still no…
3
votes
1 answer

Hack language - Set intersection

The Hack Set has a difference method but I don't see a method called intersect or any similar one. How to get an intersection of two sets? $set1 = Set { 'a', 'x' }; $set2 = Set { 'b', 'c', 'x', 'y' }; $intersection = ??? // Set { 'x' } Docs:…
Martin Konicek
  • 39,126
  • 20
  • 90
  • 98
3
votes
5 answers

PHP: Sending a list of options as an argument (alternative to named parameters/ argument bag)

I wish to give a list of options as an argument to a function. The Ideal Scenario: Named Parameters If PHP has named parameters it would be done like so: function setOptions($title, $url, $public = true, $placeholder = "type here...") { …
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
3
votes
1 answer

XHP with Regex for link Replacement

I am trying to implement a simple function that given a text input, returns the text modified with xhp_a when a link is detected, within a paragraph xhp_p. Consider this class class Urlifier { protected static $reg_exUrl =…
Themis Beris
  • 980
  • 1
  • 11
  • 25
1
2
3
11 12