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

I want remove/hide HttpResponse header in Struts 1.0

I have tried below in web.xml but no working com.sun.faces.sendPoweredByHeader false I want to do it at application level, How I can do it…
user7491136
0
votes
1 answer

Echo a php code with Jquery Ajax

i whould like to know if am able to list the content of a php file if i know exactly the location of php file. Im am hoping that myJsonData will contain the php source code. I dont know why but i know that can work and if i know exactly the…
0
votes
0 answers

Modeling Nested Data using Hack OR PHP

I'm not sure if this is even possible, but read on if you're in for a challenge: Let's say I have a table with m columns and n rows. Each subsequent column represents a depper level in a hierarchy. Something like GreatGrandParent | GrandParent |…
SimaPro
  • 1,164
  • 4
  • 14
  • 28
0
votes
2 answers

How to pass null argument to function that accept :xhp as parameter?

I have function like this : private static function myfun( string $param1, :xhp $param2, ): :xhp { return //somethinf } I don't want to pass any thing as param2. How can I do that ? When I try doing like : myfun("Hi",null),…
danish sodhi
  • 1,793
  • 4
  • 21
  • 31
0
votes
1 answer

What makes this class requirement a cyclic class definition?

The typechecker considers this class requirement by the interface IBase to be cyclic:
concat
  • 3,107
  • 16
  • 30
0
votes
0 answers

hhvm async cli hangs

I have a laravel application in which run I'm running a command line task. The task basically fetches data from the database and writes it to disk. The files are about 15-35kb in size. There are about 30 to 60 files being written for each task.…
user2108258
  • 803
  • 3
  • 13
  • 20
0
votes
1 answer

install FB CTF from scratch

Can I get how to install FBCTF Mannually. which all software is needed for that. from the scratch including server software for hack, nginx and all.
jerinisready
  • 936
  • 10
  • 24
0
votes
2 answers

Hacklang — does this type error involving `this` suggest that the underlying object type matters?

I figured the following examples would be typesafe: in all three cases, I am trying to instantiate an A that expects this, but without much luck:
concat
  • 3,107
  • 16
  • 30
0
votes
0 answers

No Hack typechecker coverage for PHP functions

I have written a fair bit of code in Hack, but the typechecker manages to surprise me. For example, the IDE complains on these two…
Janos Pasztor
  • 1,265
  • 8
  • 16
0
votes
2 answers

Parsing Hack code into Abstract Syntax Tree

I would like my Hack code to be broken down to Abstract Syntax Tree. Please advise me on available tools/libraries/github repositories which will help me to parse Hack code into AST. I have found "h2tp" (hack to php transpiler written by Facebook),…
0
votes
0 answers

Hack syntax: What does question mark followed by object operator mean?

In Hack, what is happening here and what does it mean? How do you read it? $foobar = $foo?->bar();
solimant
  • 809
  • 9
  • 15
0
votes
2 answers

How to write generic function type

I have an interface that looks like this. interface Value { public function accept(ValueVisitor $visitor): T; } Since there is only one method, I want to write a class which converts a closure into an instance of Value. final class…
Jesse
  • 6,725
  • 5
  • 40
  • 45
0
votes
1 answer

Why can't class-wide upper-bound constraints be covariant and lower-bound constraints be contravariant in Hack?

Regardless of the variance of parameter on the left side, the constraints placed on Ta and Tb in the following declaration fail the typecheck: class A<+TCov, -TCon, [±]Ta as TCov, [±]Tb super TCon> { public function __construct(private Ta $ta,…
concat
  • 3,107
  • 16
  • 30
0
votes
1 answer

sprintf() with dynamic string

I have the following code as the only way I know to convert a float to a string with the fewest possible significant digits required to reproduce it (dtoa() with mode 4 in C). $i = 14; do { $str = sprintf("%.{$i}e", $x); $i++; } while ($x !=…
Jesse
  • 6,725
  • 5
  • 40
  • 45
0
votes
1 answer

using hhvm and composer on openshift

I am building a test application on Openshift using the Nginx HHVM 3.13.1, MySQL 5.5 and phpMyAdmin 4.0 cartridges. This is a working application that I have running locally in a vagrant box that uses Composer and has a dependency on Facebook's…
Kenneth Rapp
  • 159
  • 1
  • 9