Hack is a
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
for the
HipHop Virtual Machine (HHVM), created by
Meta (formerly Facebook) as a dialect of
PHP. The language implementation is
free and open-source software
Free and open-source software (FOSS) is software available under a license that grants users the right to use, modify, and distribute the software modified or not to everyone free of charge. FOSS is an inclusive umbrella term encompassing free ...
, licensed under an
MIT License
The MIT License is a permissive software license originating at the Massachusetts Institute of Technology (MIT) in the late 1980s. As a permissive license, it puts very few restrictions on reuse and therefore has high license compatibility.
Unl ...
.
Hack allows use of both
dynamic typing
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
and
static typing
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
. This kind of a
type system
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
is called
gradual typing, which is also implemented in other programming languages such as
ActionScript
ActionScript is an object-oriented programming language originally developed by Macromedia Inc. (later acquired by Adobe). It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript (mean ...
. Hack's type system allows types to be specified for
function arguments
An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persua ...
, function
return values, and
class properties; however, types of
local variable
In computer science, a local variable is a variable that is given ''local scope''. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with ...
s are always inferred and cannot be specified.
History
Hack was introduced on March 20, 2014. 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
In computing, a script is a relatively short and simple set of instructions that typically automation, automate an otherwise manual process. The act of writing a script is called scripting. A scripting language or script language is a programming ...
that has a focus on
web development and can be embedded into
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
. 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.
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.
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. However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.
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
opening markup tag and forbids using top-level declarations. 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
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.
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
See also
* Phalanger
References
External links
* and
Facebook Q&A: Hack brings static typing to PHP world
A tutorial on converting an existing PHP project to Hack
KPHP: An Executable Formal Semantics for PHP
{{Programming languages
2014 software
Dynamically typed programming languages
Facebook
PHP software
Programming languages compiled to bytecode
Programming languages created in 2014
Scripting languages
Statically typed programming languages
Articles with example PHP code