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

Is code written in Hack faster than code written in PHP on HHVM?

Can we expect a speed gain by transitioning from PHP to Hack on HHVM? I'm thinking of the strongly typed parameters / return types, in particular scalars, does that allow HHVM to do a better job at compiling the code to native code, or is the speed…
BenMorel
  • 34,448
  • 50
  • 182
  • 322
22
votes
2 answers

How would you migrate from PHP to Hack?

Facebook have introduced a new programming language, which looks mostly like an extension to PHP. They've called it Hack and it's running on their HHVM engine. After seeing their website and reading a bit about it, I wondered how fluid a migration…
Ronni Egeriis Persson
  • 2,209
  • 1
  • 21
  • 43
16
votes
3 answers

What IDEs have support for the Hack Language?

Has anyone managed to find an IDE for Hack Language used on HHVM ?Is there any plugin available for the well-known IDEs like Eclipse,Netbeans,etc? Thank you! Edit : By the time SiebelsTim has embedded typechecker and made a basic syntax highlighting…
Themis Beris
  • 980
  • 1
  • 11
  • 25
15
votes
2 answers

Frameworks for hacklang?

Hacklang may still be relatively new but are there any frameworks (MVC for instance) that are well-documented for it ? I've stumbled accross Fastuc or Hack-mvc but they don't seem ready yet and the documentation is quite scarce; Any thoughts…
tchap
  • 3,412
  • 3
  • 29
  • 46
10
votes
2 answers

Shapes in hacklang

I have started learning hacklang today and now I am a bit stuck on shapes: http://docs.hhvm.com/manual/en/hack.shapes.php I understand the concept of shapes and it seems really useful for me, but I can't understand why for example this code does not…
WebDevHere
  • 119
  • 1
  • 7
10
votes
1 answer

What is the difference between Hack and PHP?

I left the PHP world for few years and I was working in C#.Net since then. I am thinking about diving into the PHP again and today I came across http://hhvm.com/ and it seems way more similar to C# than pure PHP. So my question is what is the…
Vojtech B
  • 2,837
  • 7
  • 31
  • 59
7
votes
1 answer

Common php functions in hack

I decided to start a new project to get into hacklang, and after fixing some if the problems I initially ran into transitioning from php habits, I ran into the following errors: Unbound name: str_replace Unbound name: empty Doing some research I…
Tiksi
  • 73
  • 1
  • 4
7
votes
1 answer

How to Use Mysql PDO driver with hacklang/hhvm

I am getting a pdo exception "could not find driver" when trying to access MySql through PDO using hacklang/hhvm (compiled today). I tried installing php5-mysql but I'm not even sure that is helpful because I'm not sure of the relationship between…
tslater
  • 4,362
  • 3
  • 23
  • 27
6
votes
2 answers

When would I use a Hack dict over a PHP array?

The docs are pretty light. The interface looks almost identical to an array. When should I prefer a dict?
hurrymaplelad
  • 26,645
  • 10
  • 56
  • 76
6
votes
3 answers

HHVM - How to use hh_client properly

So I was excited to jump into some Hacklang and HHVM, and after finally managing to get it running on a CentOS 6 VM (tip: servergrove.com have a RPM from a repo of theirs, but it still needed MySQL 5.0 shared libraries to work - still the closest I…
ndavison
  • 185
  • 7
6
votes
1 answer

Hacklang: How do I type-annotate a local/global variable?

The Hack manual makes it fairly clear how to type-annotate function parameters, function return types, and member variables. However, all of the ways I've tried to type-annotate a global variable or function-local variable result in a syntax…
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
6
votes
1 answer

HACK Lambda Example -- what?

Can someone explain how this works? http://docs.hhvm.com/manual/en/hack.lambda.php Variables are captured automatically and transitively (including $this): $y ==> $x * $z + $y; $bar = $foo(5); var_dump($bar(4)); //…
Bob Brunius
  • 1,344
  • 5
  • 14
  • 21
5
votes
1 answer

What is the top type in the Hack language?

In the Hack language type system, is there a "top" type, also known as an "any" type, or a universal "Object" type? That is, a type which all types are subclasses of? The manual mentions "mixed" types, which might be similar, but are not really…
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
5
votes
1 answer

Hacklang command line program in strict mode

Is there any way to create command line programs in strict mode of Hack? As Hack's strict mode does not allow statements outside a function I can't call my main function. (This is rather a theoretical question as it is easy to circumvent the problem…
Csq
  • 5,775
  • 6
  • 26
  • 39
5
votes
1 answer

HHVM+Hacklang: errors/warnings output into browser

Is there any way to tell HHVM to output Hacklang warnings and errors into the browser? Something like PHP does with enabled display_errors, display_startup_errors and error_reporting set to E_ALL HHVM version: $ php -v HipHop VM…
meta-nickname
  • 83
  • 1
  • 4
1
2 3
11 12