Hack (programming language)#Syntax and semantics

{{short description|Programming language}}

{{distinguish|Haxe}}

{{Infobox programming language

| name = Hack

| logo = Hack (programming language) logo.svg

| logo size = 100px

| logo alt = Hack logo, featuring white lowercase "hack" letters on a black background, with stylized triangular geometric shapes on the left

| designer = Julien Verlaguet, Alok Menghrajani, Drew Paroski, others{{cite web |url=http://www.serpentine.com/blog/2014/03/28/where-credit-belongs-for-hack/ |last1=O'Sullivan |first1=Bryan |title=Where Credit Belongs for Hack |date=2014-03-28 |access-date=2019-02-06 |archive-date=2021-03-01 |archive-url=https://web.archive.org/web/20210301030703/http://www.serpentine.com/blog/2014/03/28/where-credit-belongs-for-hack/ |url-status=live }}

| developer = Meta Platforms

| paradigms = Multi-paradigm: imperative, functional, object-oriented, procedural, reflective

| family = PHP

| released = {{Start date and age|2014}}

| latest release version = {{wikidata|property|edit|P548=Q2804309|P348}}

| latest release date = {{start date and age|{{wikidata|qualifier|single|P548=Q2804309|P348|P577}}}}

| typing = Static, dynamic, weak, gradual

| programming language =

| platform =

| license = MIT{{cite web

| url = https://github.com/facebook/hhvm/blob/master/hphp/hack/LICENSE

| title = facebook/hhvm: hhvm, hphp, hack, License

| date = 2018-04-11

| website = GitHub.com

| publisher = Meta Platforms

| access-date = 2019-02-06

| archive-date = 2019-01-07

| archive-url = https://web.archive.org/web/20190107074308/https://github.com/facebook/hhvm/blob/master/hphp/hack/LICENSE%20

| url-status = live

}}

| operating system = Cross-platform

| website = {{URL|hacklang.org}}

| influenced by = PHP, OCaml, Java, C#, Scala, Haskell

}}

Hack is a programming language 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, licensed under an MIT License.{{cite web

| url = http://radar.oreilly.com/2014/04/facebooks-hack-hhvm-and-the-future-of-php.html

| title = Facebook's Hack, HHVM, and the future of PHP

| date = 2014-04-03

| last1 = Lockhart |first1 = Josh

| publisher = O'Reilly Media

| access-date = 2019-02-06

| archive-date = 2019-01-07

| archive-url = https://web.archive.org/web/20190107074312/http://radar.oreilly.com/2014/04/facebooks-hack-hhvm-and-the-future-of-php.html

| url-status = live

}}{{cite web

| url = https://www.wired.com/wiredenterprise/2014/03/facebook-hack/

| title = Facebook Introduces 'Hack,' the Programming Language of the Future

| date = 2014-03-20

| author = Cade Metz

| publisher = Wired

| access-date = 2019-02-06

| archive-date = 2014-03-28

| archive-url = https://web.archive.org/web/20140328124125/http://www.wired.com/wiredenterprise/2014/03/facebook-hack

| url-status = live

}}

Hack allows use of 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.{{cite web

| url = https://www.cs.umd.edu/~avik/papers/iogti.pdf

| title = The Ins and Outs of Gradual Type Inference

| date = January 2012

| author1 = Aseem Rastogi

| author2 = Avik Chaudhuri

| author3 = Basil Hosmer

| publisher = Association for Computing Machinery (ACM)

| access-date = 2019-02-06

| archive-date = 2017-08-12

| archive-url = https://web.archive.org/web/20170812040858/https://www.cs.umd.edu/~avik/papers/iogti.pdf

| url-status = live

}} 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.{{cite web

| url = https://docs.hhvm.com/hack/types/annotations

| title = Hack Manual: Hack and HHVM – Type Annotations

| website = docs.hhvm.com

| access-date = 2019-02-06

| archive-date = 2018-08-02

| archive-url = https://web.archive.org/web/20180802011242/https://docs.hhvm.com/hack/types/annotations

| url-status = live

}}

History

Hack was introduced on March 20, 2014.{{cite web

| url=https://code.fb.com/developer-tools/hack-a-new-programming-language-for-hhvm/

| title=Hack: a new programming language for HHVM

| first1=Julien

| last1=Verlaguet

| first2=GANDHI

| last2=Menghrajani

| publisher=Facebook

| date=2014-03-20

| access-date=2019-02-06

| archive-date=2019-02-09

| archive-url=https://web.archive.org/web/20190209124350/https://code.fb.com/developer-tools/hack-a-new-programming-language-for-hhvm/

| url-status=live

}} 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.{{cite web

| url = https://docs.hhvm.com/hhvm/inconsistencies/introduction

| title = Inconsistencies: Introduction

| website = docs.hhvm.com

| access-date = 2019-04-04

| archive-date = 2019-04-04

| archive-url = https://web.archive.org/web/20190404125609/https://docs.hhvm.com/hhvm/inconsistencies/introduction

| url-status = dead

}}

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.{{cite web

| url = https://docs.hhvm.com/hack/typechecker/modes#partial-mode

| title = Hack Manual: Partial Mode

| website = docs.hhvm.com

| access-date = 2019-02-06

| archive-date = 2018-11-19

| archive-url = https://web.archive.org/web/20181119215731/https://docs.hhvm.com/hack/typechecker/modes#partial-mode

| url-status = live

}} However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.{{cite web

| url = https://docs.hhvm.com/hack/typechecker/modes#strict-mode

| title = Hack Manual: Strict Mode

| website = docs.hhvm.com

| access-date = 2019-02-06

| archive-date = 2018-11-19

| archive-url = https://web.archive.org/web/20181119215731/https://docs.hhvm.com/hack/typechecker/modes#strict-mode

| url-status = live

}}

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.{{cite web

| url=https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html

| title=HHVM 4.0.0

| date=2019-02-11

| first=Fred

| last=Emmott

| website=hhvm.com

| access-date=2019-05-02

| archive-date=2021-05-14

| archive-url=https://web.archive.org/web/20210514021100/https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html

| url-status=live

}} 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.

=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

{{Portal|Computer programming}}

References

{{Reflist}}