CSC/ECE 517 Spring 2015/ch1a 20 HA
Template:Distinguish Template:Infobox programming language
Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Facebook. The language is open source, licensed under the BSD License.<ref name="license" /><ref name="oreilly"></ref><ref></ref>
Hack is a dialect PHP that also runs on HHVM, but it 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.<ref></ref> Hack's type system allows types to be specified for function arguments, function return values, and class properties; however, types of local variables cannot be specified.<ref name="oreilly" /><ref name="hack.annotations"></ref><ref name="hack.otherrulesandfeatures.typeinference"></ref>
History
Hack was introduced on March 20, 2014.<ref></ref> 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.
Features
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.<ref name="unsupported"></ref>
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.<ref name="hack.annotations" /><ref name="hack.otherrulesandfeatures.typeinference" /> 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.<ref></ref> However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.<ref></ref>
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:
<?hh
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:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<!-- hh and html do not mix -->
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
This type of code is not supported by Hack; either XHP or a template engine need to be used.<ref name="unsupported" />
Functions
Hack allows types to be specified for function arguments and function return values. Functions in Hack are thus annotated with types like the following:
<?hh
// Hack functions are annotated with types.
function negate(bool $x): bool {
return !$x;
}