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
3
votes
3 answers

Code coverage tool for hack language

Is there a code coverage tool for Hack language (on hhvm)? This question is not about code coverage on PHP source code running on hhvm (which is possible using PHPUnit for example), but to generate code coverage of source code written in hack…
farzad
  • 8,775
  • 6
  • 32
  • 41
3
votes
0 answers

Start Python asyncio courutines execution immediately

I am trying to understand Python asyncio module. It seems to be much more complicated than similar functionalities in other languages. Here is an excerpt from documentation: Calling a coroutine does not start its code running – it is just a…
Piotr Jurkiewicz
  • 1,653
  • 21
  • 25
3
votes
2 answers

Codeigniter and Hack Language

I am wondering how and if it is possible to run Hack code with HHVM in codeigniter models and controllers. Also are there any active efforts to port PHP frameworks like CI into Hack?
DMin
  • 10,049
  • 10
  • 45
  • 65
3
votes
1 answer

Php Get the generic type using reflection in Hack

I am exploring Hack with HHVM, and I am using generics. I have the following Base Repository: class BaseRepository{ public function __construct(T $model){ ... } } Then I have sub-class UserRepository like so: class…
Thomas
  • 962
  • 1
  • 6
  • 12
3
votes
1 answer

HHVM Hack source to plain PHP

I searched on Google and on SO. Couldn't find this question or an answer to it anywhere. SO here goes. I'd like to start using Hack at work, at least for some small projects. Our servers right now don't have HHVM installed, and it would take some…
qrazi
  • 1,406
  • 1
  • 15
  • 18
3
votes
1 answer

Will Hack language support function-overloading polymorphism?

I was trying to figure out how to implement a Visitor pattern in Hack. It obviously requires function-overloading polymorhism, but as I have tested, this examle:
Maciej Sz
  • 11,151
  • 7
  • 40
  • 56
3
votes
2 answers

Request parameters in PHP on HHVM

I'm running HHVM 3.2.0 and trying to get access to GET and POST request parameters. The problem is, HHVM doesn't support access to PHP superglobals ($_GET, $_POST, $_SERVER, etc). The only other way I know of getting access to request parameters in…
Libbux
  • 1,711
  • 2
  • 12
  • 14
3
votes
2 answers

Generics type parameter wildcard

I would like to create an abstract class which takes a type parameter and the constructor of that class should be passed another Action eg. abstract class Action { public function __construct(private ?Action<*> $onSuccess = null) {} } How…
Tino
  • 325
  • 1
  • 8
3
votes
1 answer

Linear types in hacklang: Statically forcing an order of function calls

So Hacklang is out with a new, fancy type system, where a nullable variable has to be checked before it can be used. What I wonder is, can you achieve something like linear types, statically forcing an order of function calls, the common example…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
3
votes
2 answers

Writing a IoC container in Hack

I'm in the process of converting a PHP project to Hack, and I've come across a bit of a road block. What I'm trying to do is rewrite a IoC container from PHP to Hack, and I'm having a little trouble getting everything to pass the Hack type checker…
ndavison
  • 185
  • 7
2
votes
0 answers

How can I deprecate a class on hacklang?

I have an old class and I want to deprecate it. I added a deprecation as Phpdoc: /** * @deprecated use MyNewClass. */ class MyDeprecatedClass { public static function doDeprecatedThings() {...} ... } Which is pretty soft. I like to have a…
vmariano
  • 473
  • 2
  • 19
2
votes
1 answer

How to append to darray with pipe in Hacklang

I am trying to add an item into a darray |> darray(JSON::decodeMap($$)) I want to append one more item discover_arg => xxxxx in to this darray, how could I achieve so?
Pythoner
  • 5,265
  • 5
  • 33
  • 49
2
votes
1 answer

how to fix this HH FIXME [4410]?

Trying to fix a HH complain... Basically the code is doing something similar to this Sfirstgroup = idx($largegroup, "first"); $final_thing = null if(HH\is_any_array(Sfirstgroup) && Sfirstgroup){ /*HH_FIXME[4110] Error revealed by is_arry…
new B
  • 21
  • 1
2
votes
1 answer

How to place an if statement inside a concurrent block in Hacklang?

Say I have 3 asynchronous functions A,B, and C. I want to run them concurrently inside a concurrent block, but for one of the functions I want to check a condition. I'm looking for the following behaviour: concurrent { await A(...); await…
kebab-case
  • 1,732
  • 1
  • 13
  • 24
2
votes
1 answer

HHClient throw Unbound name error for functions in HSL

I am pretty new to hacklang. I have a piece of code (copied from the HSL github page) use namespace HH\Lib\{Vec,Dict,Keyset,Str,Math}; function main(vec $foo): vec { return $foo |> Vec\filter_nulls($$) |> Vec\map($$, $it…
Abhik
  • 1,920
  • 7
  • 27
  • 50
1 2
3
11 12