Hack (programming language)

From Infogalactic: the planetary knowledge core
Jump to: navigation, search
Hack logo, featuring white lowercase "hack" letters on a black background, with stylized triangular geometric shapes on the left
Designed by Julien Verlaguet, Alok Menghrajani, Drew Paroski, and others[1]
Developer Facebook
First appeared 2014
Typing discipline static, dynamic, weak
OS Cross-platform
License BSD License[2]
Website hacklang.org
Influenced by
PHP, OCaml, Java, C#, Scala

Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Facebook as a dialect of PHP. The language implementation is open-source, licensed under the BSD License.[2][3][4]

Hack allows programmers to use both dynamic typing and static typing. This kind of a type system is called gradual typing, which is also implemented in other programming languages such as ActionScript.[5] Hack's type system allows types to be specified for function arguments, function return values, and class properties; however, types of local variables are always inferred and cannot be specified.[3][6]


Hack was introduced on March 20, 2014.[7] Before the announcement of the new programming language, Facebook had already implemented the code and "battle tested" it on a large portion of its web site.


Hack interoperates seamlessly with PHP, which is a widely used open-source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. A majority of the valid PHP scripts is also valid in Hack; however, numerous less frequently used PHP features and language constructs are not supported in Hack.[8]

Hack extends the type hinting available in PHP 5 through the introduction of static typing, by adding new type hints (for example, for scalar types such as integer or string), as well as by extending the use of type hints (for example, for class properties or function return values). However, types of local variables cannot be specified.[6] Since Hack uses a gradual typing system, in the default mode, type annotations are not mandatory even in places they cannot be inferred; the type system will assume the author is correct and admit the code.[9] However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.[10]

Syntax and semantics

The basic file structure of a Hack script is similar to a PHP script with a few changes. A Hack file starts with <?hh as opposed to <?php for a PHP script:

echo 'Hello World';

The above script, similar to PHP, will be executed and the following output is sent to the browser:

Hello World

An important point to note is that unlike PHP, Hack and HTML code do not mix. Normally you can mix PHP and HTML code together in the same file, like this:

        <title>PHP Test</title>
        <!-- hh and html do not mix -->
        <?php echo '<p>Hello World</p>'; ?> 

This type of code is not supported by Hack; either XHP or another template engine needs to be used.[8]


Hack allows types to be specified for function arguments and function return values. Functions in Hack are thus annotated with types like the following:

// Hack functions are annotated with types.
function negate(bool $x): bool {
    return !$x;

See also


  1. "Where Credit Belongs for Hack". Bryan O'Sullivan. 2014-03-28. Retrieved 2015-02-02.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  2. 2.0 2.1 "facebook/hhvm: hhvm / hphp / hack / LICENSE". github.com. Facebook. 2014-03-20. Retrieved 2014-08-11.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  3. 3.0 3.1 Josh Lockhart (2014-04-03). "Facebook's Hack, HHVM, and the future of PHP". O'Reilly Media. Retrieved 2014-08-02.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  4. Cade Metz (2014-03-20). "Facebook Introduces 'Hack,' the Programming Language of the Future". Wired. Retrieved 2014-04-15.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  5. Aseem Rastogi; Avik Chaudhuri; Basil Hosmer (January 2012). "The Ins and Outs of Gradual Type Inference" (PDF). Association for Computing Machinery (ACM). Retrieved 2014-09-23.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  6. 6.0 6.1 "Hack Manual: Hack and HHVM – Type Annotations". docs.hhvm.com. Retrieved 2015-12-29.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  7. "Hack: a new programming language for HHVM". code.facebook.com. Facebook. Retrieved 2014-03-23.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  8. 8.0 8.1 "Hack Manual: Hack and HHVM – Unsupported PHP Features in Hack". docs.hhvm.com. Retrieved 2014-04-02.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  9. "Hack Manual: Partial Mode". docs.hhvm.com. Retrieved 2015-12-29.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  10. "Hack Manual: Strict Mode". docs.hhvm.com. Retrieved 2015-12-29.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>

External links