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

Hacklang: how to get stacktrace from Exception?

How do I get the stacktrace from an Exception object? I am specifically looking to extract the call stack and line numbers, given an Exception. I tried this: function do_it(int $x, int $y): void { try { $result = $x / $y; } catch…
nz_21
  • 6,140
  • 7
  • 34
  • 80
2
votes
1 answer

How to return a placeholder value in Hack?

I am implementing a function that returns an Awaitable- but for the moment, I just want to return a placeholder value. How do I accomplish this? In rust, I'd so something like invoke the unimplemented!() macro - in java/python, I'd return null…
nz_21
  • 6,140
  • 7
  • 34
  • 80
2
votes
1 answer

How check if shape vector contains a shape with a certain value

shape x { ?'a' => ?string, ?'b' => ?string, } I have an array of shape x, And I am trying to see if any of the shapes have a value of 'hello' in field 'a'. How can i do so using built in functions?
2
votes
1 answer

How to check if a mixed type argument is a vector of integers in HackLang?

The function below is not real, for demo only: function acceptHackArray(mixed $x):someType { .... // Need to check is $x is a vector of integers $tmp = '$x is vector'; ... return something; }
erol yeniaras
  • 3,701
  • 2
  • 22
  • 40
2
votes
2 answers

Using Dynamic Variable Names to Call Static Variable in PHP

I am trying to implement a logging library which would fetch the current debug level from the environment the application runs in: 23 $level = $_SERVER['DEBUG_LEVEL']; 24 $handler = new StreamHandler('/var/log/php/php.log',…
user3081519
  • 2,659
  • 5
  • 25
  • 35
2
votes
1 answer

Hacklang — Are type constraints with type constants possible to use?

In experimenting with constraints on generic functions in Hack, I conjured up the following:
concat
  • 3,107
  • 16
  • 30
2
votes
2 answers

Memoize attribute in the Hack language - cache timeout

Hack has the <<__Memoize>> attribute to easily cache method results. How can I use it to cache results of some database or API request for a limited amount of time? Let's say my code very frequently needs some information from a database: public…
Martin Konicek
  • 39,126
  • 20
  • 90
  • 98
2
votes
1 answer

HackLang by Facebook is not strict

Good day, I have problem. I want to simulate some errors in hacklang. { return $this->vector; } …
JaSHin
  • 211
  • 2
  • 16
  • 43
2
votes
1 answer

hhvm hack lang error 500 instead error report

I played around with hack on an nginx dockered service. Everything is fine instead of provoke errors. For example this function: echo add_one("1"); function add_one(int $x): int { return $x+1; } This should produce an error because it is no…
bdart
  • 745
  • 6
  • 18
2
votes
2 answers

Change collation of AsyncMysqlClient

How can I change the collation of AsyncMysqlClient (or AsyncMysqlConnection, I am not sure which one of them) to utf8? I read the documentation, but I cannot find any method for changing the charset. I am probably missing it, if it's actually…
Victor
  • 13,914
  • 19
  • 78
  • 147
2
votes
1 answer

Nuclide IDE & Remote Server Setup with Watchman

Facebook recently announced Nuclide, that supports Remote Development and Hack. I have followed all the installation instructions, but autocomplete/inline error report/click-to-definition features do not work, whereas nuclide-language-hack,…
Themis Beris
  • 980
  • 1
  • 11
  • 25
2
votes
1 answer

How to properly type a generator function in Hack

I'm playing around with Hack for a bit and tried to create a generator function using the yield keyword. The documentation states that the return type of such a function should be the Continuation interface. However, when running hh_client on the…
Sander Toonen
  • 3,463
  • 35
  • 54
2
votes
1 answer

SoapServer on HHVM throws a "Bad Request" SoapFault

I'm creating a Soap web service in Hacklang and while doing some tests I've noticed that in some cases I get a SoapFault - Bad Request. To find out where the problem is, I wrote simplified class in php and prepared appropriate wsdl file. After some…
Luigi
  • 83
  • 5
2
votes
1 answer

Execute Hackificator From PHP

When I try to execute Facebook HHVM hackficator from PHP I get this error Fatal error: exception Failure("unstable www state before modification") I googled error and found reference inside the code written in OCaml…
Aftab Naveed
  • 3,652
  • 3
  • 26
  • 40
2
votes
1 answer

Disable typechecking from .hhconfig

Assume we have a project with the following structure: root/ .hhconfig ├── directory1 ├── directory2 ├── directory3 ......................... ├── directory10 Is there a way to have a single .hhconfig file, and exclude onlydirectory8 from the…
Themis Beris
  • 980
  • 1
  • 11
  • 25