Hack (programming language)

from Wikipedia, the free encyclopedia
Hack
logo
Basic data
Publishing year: 2014
Designer: Julien Verlaguet, Alok Menghrajani, and others
Developer: Facebook Inc.
Typing : weak , dynamic , static
Influenced by: PHP , OCaml , Java , C # , Scala , Haskell
License : BSD license
hacklang.org

Hack is a scripting language for the HipHop Virtual Machine (HHVM). Hack extends the programming language PHP u. a. about static typing and generics . Any PHP code can also be compiled using the Hack Compiler. Hack was developed by Facebook Inc. , which is gradually converting its social network web application Facebook , which was originally written in PHP, to the new language Hack, as the original use of PHP together with the Zend Engine could no longer meet the high performance requirements of Facebook. At the beginning of April 2014, the new language was officially introduced by Facebook Inc. and made freely available as open source .

syntax

Similar to PHP code there is also hack in tags, but here <? Hh?> Is used instead of <? Php?>.

<?hh
echo 'Hello World';
?>

Hack uses largely the same language constructs and functions as PHP, so the above code would generate the following output analogous to PHP:

Hello World

Features

Static typing

A core element of the language is the type check. Although PHP is used as the server-side language on around 80% of websites, many developers criticize the dynamic typing of the language, which in their opinion significantly increases the susceptibility to errors. The type checker used in Hack keeps the source code in memory and monitors the hard drive for changes to the file. As a result, the checks are possible in 200 milliseconds and do not cause any noticeable delay in programming, errors can be made visible even before the first execution. Since null is a value that does not correspond to all other types used, its use in Hack must be explicitly permitted by a preceding question mark ( Nullable ). Properties of a class must also be initialized if they are not optional. The programmer can now specify the data type in advance for variables as well as for properties and methods of classes and their parameters:

<?hh
class typing
{
     private int $zahl = 0;
     protected string $text;
     public ?string $optional;      // ...muss nicht initialisiert werden

     public function __construct()
     {
         $this->text = '';          // Jede Klasseneigenschaft muss initialisiert werden.
     }

     public function zeige_pi(): float
     {
         return round(pi(), 5);
     }

     public function ist_null(?T $wert): bool
     {
         return $wert === null;
     }

}

Generics

In addition to simple, untyped arrays , the language offers other special fields - generics . In Hack , like most other types in Hack, these field types implement an accessible interface so that methods can be called on these object-like structures. You can also create your own generics .

Vector

A vector is a strictly typed array . The type of entries is determined by the first. The type can be strictly defined down to the value level.

class Gedicht
{
     protected Vector<string> $gedicht = Vector(
           'Ein Schnupfen hockt auf der Terrasse',
           'auf daß er sich ein Opfer fasse',
           'und stürzt alsbald mit voller Grimm',
           'auf einen Menschen namens Schrimm.',
           'Paul Schrimm erwidert prompt: "Pitschü!"',
           'und hat ihn drauf bis Montag früh.');

    public function abgeben(): Vector<string>
    {
          return $this->gedicht;
    }

    public function aufsagen(): string
    {
          $rezitation = '';
          foreach($this->gedicht as $zeile)
          {
               $rezitation .= $zeile."\n";
          }

          return $rezitation;
    }
}

set

A set is an array in which each value does not appear more than once.

$karten = Set {"Bube", "Dame", "König", "Ass"};

$karten[] = "Bube"; // Ohne Effekt, denn Bube ist schon Teil des Sets.

$karten->remove("Dame");
$karten->add("Joker");

Map

A map is a collection of key-value pairs, similar to a dictionary. Unlike associative arrays, values ​​in a map retain the order in which they were inserted.

$map = Map {"A" => 1, "E" => 5, "D" => 4, "B" => 2, "C" => 3};

foreach($map as $schluessel => $wert)
{
     echo $wert."\n"; // Gibt die Werte 1, 5, 4, 2, 3 aus.
}

License and reference

Hack is distributed under the BSD license . The software can be downloaded for free from the Internet, the code is managed on GitHub .

Individual evidence

  1. Contributors to facebook / hhvm . Github.com. Retrieved March 25, 2014.

Web links