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

array_merge for Maps and Vectors at hacklang

Given the following code
mcuadros
  • 4,098
  • 3
  • 37
  • 44
1
vote
1 answer

How to get XHP working in hack/hhvm on Ubuntu

I've installed hack-nightly and set up fastcgi using nginx by following the instructions on the website, however I'm getting an error when trying to create a simple file that uses xhp: hello; echo…
Paul Johnson
  • 1,329
  • 1
  • 12
  • 25
0
votes
0 answers

Hack, HHVM, MySQL: cancelling while connecting

If I connect to my MySQL-database via Hack I get an AsyncMysqlConnectException with the message "Error executing AsyncMysql operation: Cancelled". I use AsyncMysqlConnectionPool::connect with 'localhost', '127.0.0.1', '0.0.0.0' as $host and 3306 as…
0
votes
2 answers

timeout for async calles in Hack

I am looking how to achieve what https://docs.python.org/3/library/asyncio-task.html#timeouts gives me, but in hack. I just want to timeout my async call in 30 minutes.
0
votes
1 answer

Regex to match all occurrences of pipe operator (|>) expressions in hacklang

For eg: Input -> "x() |> y($$) |> z($$) any random string a() |> b($$) |> c($$)" output -> vec[ "x() |> y($$) |> z($$)", "a() |> b($$) |> c($$)" ] // this contains two groups of expressions. I'm not sure if this is…
Abhishek
  • 1,302
  • 10
  • 18
0
votes
1 answer

"zsh: illegal hardware instruction hhvm" when running Hackland on VSCode, Macbook Pro M1

I'm trying to run a Hacklang code and also a Hacklang website on VSCode, Mac Pro M1. However, I keep getting the errors zsh: illegal hardware instruction hhvm hello.hack and zsh: illegal hardware instruction hhvm -m server -p 8080. I follow the…
0
votes
1 answer

HHVM Library Not Loaded MacOS

I tried to install hack and hhvm to my Macbook. I followed the instructions on its official documentation. I used home brew to install hhvm. However when I try to run my first hack program I got the following error: hhvm hello.hack dyld[56813]:…
0
votes
0 answers

Visual code debugger adapter immediately dies when trying to connect to vscode-hack debugger in local launch mode

I am using the vscode-hack extension on visual code studio on macOS Big Sur. My settings.json includes following launch configuration: "configurations": [ { "name": "HHVM: Run Script", "type": "hhvm", "request": "launch", "script":…
0
votes
1 answer

running minimal app with xhp-lib v4 and hhvm v 4.81.1 throws error

I'm trying the following setup and am getting this error: \nFatal error: Uncaught Error: Found top-level code in /home/user/code/xhp-simple/src/example.php:7\nStack trace:\n#0 (): main()\n#1 {main} Setup: composer.json { "require": { …
reactor
  • 1,722
  • 1
  • 14
  • 34
0
votes
1 answer

What kind of string representation is this?

I'm running the following code and I get the following output. According to the documentation it says it returns a binary string? PHP\pack('H*', 'ab') // "\253" PHP\pack('H*', 'a') // "\240" I can't for the life of me figure out 253 is…
Luke Xu
  • 2,302
  • 3
  • 19
  • 43
0
votes
0 answers

Connection to a oracle database

This one seems like it should be obvious but could I get some guidance on how you use HHVM and Hack to connect to an Oracle DB? Has anyone done this? If so could we provide some guidance on this? I am sure someone has done this in the…
George
  • 11
  • 2
0
votes
1 answer

hhvm issues \nFatal error: Uncaught Error: unknown class xhp_x__element in :\nStack trace:\n#0 /Users/user/code/xhp-js-example/example.php(12)

I'm trying to play with xhp and I'm finding that running the one xhp example I could find https://github.com/hhvm/xhp-js-example is issuing an error \nFatal error: Uncaught Error: Found top-level code in :\nStack trace:\n#0 {main} when following the…
reactor
  • 1,722
  • 1
  • 14
  • 34
0
votes
1 answer

Is it possible to install h2tp in 2018?

As far as I know Hack had h2tp utility to complile Hack code to PHP. But today I couldn't find this command in any of official prebuilt HHVM packages for Ubuntu, and Facebook's blog post link redirects to getting started page. And judging by this…
Celestine
  • 448
  • 7
  • 18
0
votes
1 answer

How to start HHVM CLI Server

HHVM 3.19 has added something called CLI server mode: https://hhvm.com/blog/2017/04/13/hhvm-3-19.html The idea of the CLI server is that some caches can be shared and that the startup costs of HHVM can be avoided. The documentation has no…
Wolph
  • 78,177
  • 11
  • 137
  • 148
0
votes
1 answer

Hacklang unexpected interface error

I am learning hacklang and having issues with using interface in different files. Here is my code. IpAuthorizedController.php
maranovot
  • 405
  • 1
  • 6
  • 17