Hack (programming language)

Last updated
Hack
Hack (programming language) logo.svg
Designed by Julien Verlaguet, Alok Menghrajani, Drew Paroski, and others [1]
Developer Meta Platforms
First appeared2014
Stable release
4.172  OOjs UI icon edit-ltr-progressive.svg / 2 November 2022;18 months ago (2 November 2022)
Typing discipline Static, dynamic, weak, gradual
OS Cross-platform
License MIT License [2]
Website 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 open-source, licensed under the MIT License. [2] [3] [4]

Contents

Hack 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. [5] 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. [3] [6]

History

Hack was introduced on March 20, 2014. [7] Before the announcement of the new programming language, Facebook had already implemented the code and tested it on a large portion of its website.

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, numerous less frequently used PHP features and language constructs are not supported in Hack. [8]

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. [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]

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>>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; 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.functionnegate(bool$x):bool{return!$x;}

See also

Related Research Articles

<span class="mw-page-title-main">PHP</span> Scripting language created in 1994

PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. PHP was originally an abbreviation of Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

OCaml is a general-purpose, high-level, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy, Ascánder Suárez, and others.

ECMAScript is a standard for scripting languages, including JavaScript, JScript, and ActionScript. It is best known as a JavaScript standard intended to ensure the interoperability of web pages across different web browsers. It is standardized by Ecma International in the document ECMA-262.

In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every term. Usually the terms are various language constructs of a computer program, such as variables, expressions, functions, or modules. A type system dictates the operations that can be performed on a term. For variables, the type system determines the allowed values of that term. Type systems formalize and enforce the otherwise implicit categories the programmer uses for algebraic data types, data structures, or other components.

Type inference, sometimes called type reconstruction, refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistics.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written or compiled in another one. An FFI is often used in contexts where calls are made into binary dynamic-link library.

In type theory, a theory within mathematical logic, the bottom type of a type system is the type that is a subtype of all other types.

The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under an MIT License. The compiler, written in OCaml, is released under the GNU General Public License (GPL) version 2.

Phalanger is a compiler front end for compiling PHP source code into CIL byte-code, which can be further processed by the .NET Framework's just-in-time compiler. The project was started at Charles University and is supported by Microsoft. Phalanger was discontinued in favor of the more modern PeachPie compiler, which utilizes the Roslyn API.

TypeScript is a free and open-source high-level programming language developed by Microsoft that adds static typing with optional type annotations to JavaScript. It is designed for the development of large applications and transpiles to JavaScript. Because TypeScript is a superset of JavaScript, all JavaScript programs are syntactically valid TypeScript, but they can fail to type-check for safety reasons.

<span class="mw-page-title-main">TCPDF</span>

TCPDF is a free and open source software PHP class for generating PDF documents. TCPDF is the only PHP-based library that includes complete support for UTF-8 Unicode and right-to-left languages, including the bidirectional algorithm.

The syntax and semantics of PHP, a programming language, form a set of rules that define how a PHP program can be written and interpreted.

<span class="mw-page-title-main">Gilad Bracha</span> American software engineer

Gilad Bracha is a software engineer at F5 Networks, and formerly at Google, where he was on the Dart programming language team. He is creator of the Newspeak language, and co-author of the second and third editions of the Java Language Specification, and a major contributor to the second edition of the Java Virtual Machine Specification.

HipHop for PHP (HPHPc) is a discontinued PHP transpiler created by Facebook. By using HPHPc as a source-to-source compiler, PHP code is translated into C++, compiled into a binary and run as an executable, as opposed to the PHP's usual execution path of PHP code being transformed into opcodes and interpreted. HPHPc consists mainly of C++, C and PHP source codes, and it is free and open-source software distributed under the PHP License.

<span class="mw-page-title-main">Rust (programming language)</span> General-purpose programming language

Rust is a multi-paradigm, general-purpose programming language that emphasizes performance, type safety, and concurrency. It enforces memory safety—meaning that all references point to valid memory—without a garbage collector. To simultaneously enforce memory safety and prevent data races, its "borrow checker" tracks the object lifetime of all references in a program during compilation.

XHP is an augmentation of PHP and Hack developed at Meta to allow XML syntax for the purpose of creating custom and reusable HTML elements. It is available as an open-source software GitHub project and as a Homebrew module for PHP 5.3, 5.4, and 5.5. Meta also developed a similar augmentation for JavaScript, named JSX.

HipHop Virtual Machine (HHVM) is an open-source virtual machine based on just-in-time (JIT) compilation that serves as an execution engine for the Hack programming language. By using the principle of JIT compilation, Hack code is first transformed into intermediate HipHop bytecode (HHBC), which is then dynamically translated into x86-64 machine code, optimized, and natively executed. This contrasts with PHP's usual interpreted execution, in which the Zend Engine transforms PHP source code into opcodes that serve as a form of bytecode, and executes the opcodes directly on the Zend Engine's virtual CPU.

<span class="mw-page-title-main">PeachPie</span>

PeachPie is an open-source PHP language compiler and runtime for the .NET Framework and .NET. It is built on top of the Microsoft Roslyn compiler platform and is based on the first-generation Phalanger project. PeachPie compiles source code written in PHP to CIL byte-code. PeachPie takes advantage of the JIT compiler component of the .NET Framework in order to handle the beginning of the compilation process. Its purpose is not to generate or optimize native code, but rather to compile PHP scripts into .NET assemblies containing CIL code and meta-data. In July 2017, the project became a member of the .NET Foundation.

References

  1. Bryan O'Sullivan (2014-03-28). "Where Credit Belongs for Hack". Archived from the original on 2021-03-01. Retrieved 2019-02-06.
  2. 1 2 "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.
  3. 1 2 Josh Lockhart (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.
  4. Cade Metz (2014-03-20). "Facebook Introduces 'Hack,' the Programming Language of the Future". Wired. Archived from the original on 2014-03-28. Retrieved 2019-02-06.
  5. 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.
  6. 1 2 "Hack Manual: Hack and HHVM – Type Annotations". docs.hhvm.com. Archived from the original on 2018-08-02. Retrieved 2019-02-06.
  7. 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.
  8. 1 2 "Inconsistencies: Introduction". docs.hhvm.com. Archived from the original on 2019-04-04. Retrieved 2019-04-04.
  9. "Hack Manual: Partial Mode". docs.hhvm.com. Archived from the original on 2018-11-19. Retrieved 2019-02-06.
  10. "Hack Manual: Strict Mode". docs.hhvm.com. Archived from the original on 2018-11-19. Retrieved 2019-02-06.
  11. Emmott, Fred (2019-02-11). "HHVM 4.0.0". hhvm.com. Archived from the original on 2021-05-14. Retrieved 2019-05-02.