Hack (programming language)
Developer Meta Platforms | | |
First appeared | 2014 | |
---|---|---|
Stable release | / 2 November 2022 | |
OS | Cross-platform | |
License | MIT[2] | |
Website | hacklang | |
Influenced by | ||
PHP, OCaml, Java, C#, Scala, Haskell |
Hack is a
Hack allows use of both
History
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.
Features
Hack is designed to interoperate seamlessly with PHP, which is a widely used open-source scripting language that has a focus on web development and can be embedded into HTML. 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 the
Syntax and semantics
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 via include
, 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 called main
:
<<__EntryPoint>>
function main(): 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; either XHP or another template engine needs to be used.[8]
Functions
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.
function negate(bool $x): bool {
return !$x;
}
See also
References
- ^ O'Sullivan, Bryan (2014-03-28). "Where Credit Belongs for Hack". Archived from the original on 2021-03-01. Retrieved 2019-02-06.
- ^ a b "facebook/hhvm: hhvm, hphp, hack, License". GitHub.com. Meta Platforms. 2018-04-11. Archived from the original on 2019-01-07. Retrieved 2019-02-06.
- ^ a b Lockhart, Josh (2014-04-03). "Facebook's Hack, HHVM, and the future of PHP". O'Reilly Media. Archived from the original on 2019-01-07. Retrieved 2019-02-06.
- Wired. Archivedfrom the original on 2014-03-28. Retrieved 2019-02-06.
- ^ Aseem Rastogi; Avik Chaudhuri; Basil Hosmer (January 2012). "The Ins and Outs of Gradual Type Inference" (PDF). Association for Computing Machinery (ACM). Archived (PDF) from the original on 2017-08-12. Retrieved 2019-02-06.
- ^ a b "Hack Manual: Hack and HHVM – Type Annotations". docs.hhvm.com. Archived from the original on 2018-08-02. Retrieved 2019-02-06.
- ^ Verlaguet, Julien; Menghrajani, GANDHI (2014-03-20). "Hack: a new programming language for HHVM". Facebook. Archived from the original on 2019-02-09. Retrieved 2019-02-06.
- ^ a b "Inconsistencies: Introduction". docs.hhvm.com. Archived from the original on 2019-04-04. Retrieved 2019-04-04.
- ^ "Hack Manual: Partial Mode". docs.hhvm.com. Archived from the original on 2018-11-19. Retrieved 2019-02-06.
- ^ "Hack Manual: Strict Mode". docs.hhvm.com. Archived from the original on 2018-11-19. Retrieved 2019-02-06.
- ^ Emmott, Fred (2019-02-11). "HHVM 4.0.0". hhvm.com. Archived from the original on 2021-05-14. Retrieved 2019-05-02.