| Hack | |
|---|---|
| Paradigms | Multi-paradigm:imperative,functional,object-oriented,procedural,reflective |
| Family | PHP |
| Designed by | Julien Verlaguet, Alok Menghrajani, Drew Paroski, others[1] |
| Developer | Meta Platforms |
| First appeared | 2014; 11 years ago (2014) |
| Stable release | |
| Typing discipline | Static,dynamic,weak,gradual |
| OS | Cross-platform |
| License | MIT[2] |
| Website | hacklang |
| Influenced by | |
| PHP,OCaml,Java,C#,Scala,Haskell | |
Hack is aprogramming language for theHipHop Virtual Machine (HHVM), created byMeta (formerly Facebook) as a dialect ofPHP. The language implementation isfree and open-source software, licensed under anMIT License.[2][3][4]
Hack allows use of bothdynamic typing andstatic typing. This kind of atype system is calledgradual typing, which is also implemented in other programming languages such asActionScript.[5] Hack's type system allows types to be specified forfunctionarguments, functionreturn values, andclass properties; however, types oflocal variables are always inferred and cannot be specified.[3][6]
Hack was introduced on March 20, 2014.[7] Before the announcement of the new language, Facebook had already implemented the code and tested it on a large part of its web site.
Hack is designed to interoperate seamlessly with PHP, which is a widely used open-sourcescripting language that has a focus onweb development and can be embedded intoHTML. A majority of valid PHP scripts are also valid in Hack; however, many less-often used PHP features and language constructs are unsupported in Hack.[8]
Hack extends thetype 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]
The basic file structure of a Hack script is similar to a PHP script with a few changes. A Hack file does not include the<?php opening markup tag and forbids using top-level declarations.[11] Code must be placed in an entrypoint function. These are automatically executed if they are in the top-level file, but not if the file is included viainclude,require, or the autoloader. Like other functions in Hack, the function names must be unique within a project – i.e., projects with multiple entrypoints can not both be calledmain:
<<__EntryPoint>>functionmain():void{echo'Hello, World!';}
The above script, similar to PHP, will be executed and the following output is sent to the browser:
Hello, World!Unlike PHP, Hack and HTML code do not mix; eitherXHP or anothertemplate engine needs to be used.[8]
Like PHP 7, 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.functionnegate(bool$x):bool{return!$x;}