2015-02-17 12:45:04 +00:00
|
|
|
---
|
|
|
|
language: Hack
|
|
|
|
contributors:
|
2022-07-11 02:52:18 +00:00
|
|
|
- ["Andrew DiMola", "https://github.com/AndrewDiMola"]
|
2015-02-17 12:45:04 +00:00
|
|
|
- ["Stephen Holdaway", "https://github.com/stecman"]
|
2015-10-04 14:55:16 +00:00
|
|
|
- ["David Lima", "https://github.com/davelima"]
|
2015-02-17 12:45:04 +00:00
|
|
|
filename: learnhack.hh
|
|
|
|
---
|
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
[Hack](https://hacklang.org/) lets you write code quickly, while also having safety features built in, like static typechecking.
|
|
|
|
|
|
|
|
To run Hack code, [install HHVM](https://docs.hhvm.com/hhvm/installation/introduction), the open-source virtual machine.
|
|
|
|
|
2022-07-19 18:37:35 +00:00
|
|
|
```php
|
2022-07-11 02:52:18 +00:00
|
|
|
/* ==================================
|
|
|
|
* READ THE DOCS!
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* For more information on the Hack language:
|
|
|
|
* - About Hack: https://hacklang.org/
|
|
|
|
* - Documentation: https://docs.hhvm.com/hack/
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* A NOTE ON PHP
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// The Hack language began as a superset of PHP.
|
|
|
|
// Since then, the languages have (largely) diverged.
|
|
|
|
// You may encounter the .php extension, which is no longer recommended.
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* COMMENTS
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Hack has single-line comments...
|
|
|
|
|
|
|
|
/* Multi-line comments...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ... and a special syntax for doc comments.
|
|
|
|
*
|
|
|
|
* Use doc comments to summarize the purpose of a definition, function, class or method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* NAMESPACES
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Namespaces contain definitions of classes, interfaces, traits, functions, and constants.
|
|
|
|
|
|
|
|
namespace LearnHackinYMinutes {
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* TYPES
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
function demo_hack_types(): void {
|
|
|
|
|
2022-07-11 22:44:32 +00:00
|
|
|
// Hack has five primitive types: bool, int, float, string, and null.
|
2022-07-11 02:52:18 +00:00
|
|
|
$is_helpful = true; // bool
|
|
|
|
$int_value = 10; // int
|
|
|
|
$precise_value = 2.0; // float
|
|
|
|
$hello_world = "Hello World!"; // string
|
|
|
|
$null_string = null; // null
|
|
|
|
|
|
|
|
// Create a `shape` with the shape keyword, with a series of field names and values.
|
|
|
|
$my_point = shape('x' => -3, 'y' => 6, 'visible' => true);
|
|
|
|
|
|
|
|
// Create a `tuple` with the tuple keyword, with a series of two or more types as values.
|
|
|
|
$apple_basket = tuple("apples", 25); // different types are OK
|
|
|
|
|
|
|
|
// Use `arraykey` to represent either an integer or string.
|
|
|
|
$the_answer = 42;
|
2022-07-11 03:06:24 +00:00
|
|
|
$is_answer = process_key($the_answer);
|
2022-07-11 02:52:18 +00:00
|
|
|
|
2022-07-16 00:52:44 +00:00
|
|
|
// Similarly, `num` represents either an int or float.
|
2022-07-11 02:52:18 +00:00
|
|
|
$lucky_number = 7;
|
|
|
|
$lucky_square = calculate_square($lucky_number);
|
|
|
|
}
|
|
|
|
|
2022-07-11 03:06:24 +00:00
|
|
|
function process_key(arraykey $the_answer): bool {
|
2022-07-11 02:52:18 +00:00
|
|
|
if ($the_answer is int) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
} // true
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculate_square(num $arg)[]: float {
|
|
|
|
return ((float)$arg * $arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enums are limited to int or string (as an Arraykey), or other enum values.
|
|
|
|
enum Permission: string {
|
|
|
|
Read = 'R';
|
|
|
|
Write = 'W';
|
|
|
|
Execute = 'E';
|
|
|
|
Delete = 'D';
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:44:32 +00:00
|
|
|
// In contrast, an enum class can be of any value type!
|
2022-07-16 00:52:44 +00:00
|
|
|
enum class Random: mixed {
|
2022-07-11 22:44:32 +00:00
|
|
|
int X = 42;
|
|
|
|
string S = 'foo';
|
|
|
|
}
|
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
/* ==================================
|
|
|
|
* HACK ARRAYS
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
2022-07-16 00:52:44 +00:00
|
|
|
// The following line lets us use functions in the `C\` namespace.
|
|
|
|
use namespace HH\Lib\C; // the `C` library operates on containers
|
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
function demo_hack_arrays(): void {
|
|
|
|
|
2022-07-16 00:52:44 +00:00
|
|
|
// vec: ordered
|
2022-07-11 02:52:18 +00:00
|
|
|
$v = vec[1, 2, 3];
|
|
|
|
$letters = vec['a', 'b', 'c'];
|
2022-07-16 00:52:44 +00:00
|
|
|
|
|
|
|
$letters[0]; // returns 'a'
|
2022-07-11 02:52:18 +00:00
|
|
|
$letters[] = 'd'; // appends 'd'
|
|
|
|
|
2022-07-16 00:52:44 +00:00
|
|
|
// `inout` provides pass-by-reference behavior
|
|
|
|
C\pop_back(inout $letters); // removes 'd'
|
|
|
|
C\pop_front(inout $letters); // removes 'a'
|
|
|
|
|
|
|
|
// keyset: ordered, without duplicates
|
2022-07-11 02:52:18 +00:00
|
|
|
$k = keyset[1, 2, 3]; // values must be int or string
|
|
|
|
$colors = keyset['red', 'blue', 'green'];
|
2022-07-16 00:52:44 +00:00
|
|
|
|
|
|
|
// keyset keys are identical to their values
|
|
|
|
$colors['blue']; // returns 'blue'.
|
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
$colors[] = 'yellow'; // appends 'yellow'
|
|
|
|
unset($colors['red']); // removes 'red'
|
|
|
|
|
|
|
|
// dict: ordered, by key-value
|
|
|
|
$d = dict['a' => 1, 'b' => 3]; // keys must be int or string
|
|
|
|
$alphabet = dict['a' => 1, 'b' => 2];
|
2022-07-16 00:52:44 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
$alphabet['a']; // indexing at 'a' returns `1`
|
|
|
|
$alphabet['c'] = 3; // adds a new key-value pair of `c => 3`
|
2022-07-16 00:52:44 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
unset($alphabet['b']); // removes 'b'
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* THE HACK STANDARD LIBRARY (HSL)
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
2022-07-11 22:44:32 +00:00
|
|
|
// The Hack Standard Library is a set of functions and classes for the Hack language.
|
|
|
|
// Namespace use declarations are ideally at the top of your file but are placed here for instruction purposes.
|
2022-07-11 02:52:18 +00:00
|
|
|
|
|
|
|
use namespace HH\Lib\Str; // The `Str` library operates on strings
|
|
|
|
|
|
|
|
function demo_hack_standard_library(): void {
|
|
|
|
|
|
|
|
$letters = vec['a', 'b', 'c'];
|
|
|
|
$colors = keyset['red', 'blue', 'green'];
|
|
|
|
$alphabet = dict['a' => 1, 'b' => 2];
|
|
|
|
|
|
|
|
C\contains($letters, 'c'); // checks for a value; returns 'true'
|
|
|
|
C\contains($colors, 'purple'); // checks for a value; returns 'false'
|
2022-07-11 22:44:32 +00:00
|
|
|
C\contains_key($alphabet, 'a'); // checks for a key; returns 'true'
|
|
|
|
C\contains($alphabet, 'd'); // checks for a value; returns 'false'
|
2022-07-11 02:52:18 +00:00
|
|
|
|
|
|
|
Str\length("foo"); // returns `3`
|
|
|
|
Str\join(vec['foo', 'bar', 'baz'], '!'); // returns `foo!bar!baz`
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* HELLO WORLD!
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
use namespace HH\Lib\IO; // the `IO` library is a standard API for input / output
|
|
|
|
|
|
|
|
<<__EntryPoint>> // required attribute for the typical entry/main function
|
|
|
|
async function main(): Awaitable<
|
|
|
|
void,
|
|
|
|
> { // does not need to be named 'main' / is an asynchronous function
|
|
|
|
await IO\request_output()->writeAllAsync(
|
|
|
|
"Hello World!\n",
|
|
|
|
); // prints 'Hello World'!
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* FUNCTIONS
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Functions are defined globally.
|
|
|
|
// When a function is defined in a class, we refer to the function as a method.
|
|
|
|
|
2022-07-11 22:44:32 +00:00
|
|
|
// Functions have return types (here: `int`) and must return a value of
|
|
|
|
// that type or return no value when a void return type annotation was used.
|
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
function add_one(int $x): int {
|
|
|
|
return $x + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions can also have defined, default values.
|
|
|
|
function add_value(int $x, int $y = 1): int {
|
|
|
|
return $x + $y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions can be variadic (unspecified length of arguments).
|
|
|
|
function sum_ints(int $val, int ...$vals): int {
|
|
|
|
$result = $val;
|
|
|
|
|
|
|
|
foreach ($vals as $v) {
|
|
|
|
$result += $v;
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
2022-07-11 02:52:18 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions can also be anonymous (defined with the `==>` arrow).
|
|
|
|
// $f = (int $x): int ==> $x + 1;
|
|
|
|
|
2022-07-16 00:52:44 +00:00
|
|
|
/* ==================================
|
|
|
|
* PIPE OPERATOR
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// The pipe operator, `|>`, evaluates the result of a left-hand expression
|
|
|
|
// and stores the result in `$$`, the predefined pipe variable.
|
|
|
|
|
|
|
|
use namespace HH\Lib\Vec;
|
|
|
|
|
|
|
|
function demo_pipe_operator(): void {
|
|
|
|
|
|
|
|
Vec\sort(Vec\map(vec[2, 1, 3], $a ==> $a * $a)); // vec[1,4,9]
|
|
|
|
|
|
|
|
// the same result, but using the pipe operator and pipe variable:
|
|
|
|
$x = vec[2, 1, 3]
|
|
|
|
|> Vec\map($$, $a ==> $a * $a) // $$ with value vec[2,1,3]
|
|
|
|
|> Vec\sort($$); // $$ with value vec[4,1,9]
|
|
|
|
}
|
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
/* ==================================
|
|
|
|
* ATTRIBUTES
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Hack provides built-in attributes that can change runtime or static type checking behavior.
|
|
|
|
// For example, we used the `__EntryPoint` attribute earlier in the "Hello World!" example.
|
|
|
|
|
|
|
|
// As another example, `__Memoize` caches the result of a function.
|
|
|
|
<<__Memoize>>
|
2022-07-11 22:44:32 +00:00
|
|
|
async function do_expensive_task(): Awaitable<string> {
|
|
|
|
$site_contents = await \HH\Asio\curl_exec("http://hacklang.org");
|
|
|
|
return $site_contents;
|
2022-07-11 02:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* CONTEXTS
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Hack functions are attached to different contexts and capabilities.
|
|
|
|
// A context is a grouping of capabilities; that is, a grouping of permissions.
|
|
|
|
|
|
|
|
// To declare allowed contexts (and capabilities), use the Context List `[]`.
|
|
|
|
// If contexts are not defined, your function includes permissions defined in Hack's `defaults` context.
|
|
|
|
|
|
|
|
// Because the context list is NOT defined, the `defaults` context is implicitly declared.
|
|
|
|
async function implicit_defaults_context(): Awaitable<void> {
|
|
|
|
await IO\request_output()->writeAllAsync(
|
|
|
|
"Hello World!\n",
|
|
|
|
); // prints 'Hello World'!
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the function below, the context list is defined to have the `defaults` context.
|
|
|
|
// A function can have multiple contexts [context1, context2, ...].
|
|
|
|
// `defaults` includes most of the capabilities defined by the Hack language.
|
|
|
|
async function explicit_defaults_context()[defaults]: Awaitable<void> {
|
|
|
|
await IO\request_output()->writeAllAsync("Hello World!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can also specify zero contexts to create a pure function (no capabilities).
|
|
|
|
async function empty_context()[]: Awaitable<void> {
|
|
|
|
// The following line is an error, as the function does not have IO capabilities.
|
|
|
|
// await IO\request_output()->writeAllAsync("Hello World!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* GENERICS
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Generics allow classes or methods to be parameterized to any set of types.
|
|
|
|
// That's pretty cool!
|
|
|
|
|
|
|
|
// Hack typically passes by value: use `inout` to pass by reference.
|
|
|
|
function swap<T>(inout T $input1, inout T $input2): void {
|
|
|
|
$temp = $input1;
|
|
|
|
$input1 = $input2;
|
|
|
|
$input2 = $temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* CLASSES
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Classes provide a way to group functionality and state together.
|
|
|
|
// To define a class, use the `class` keyword. To instantiate, use `new`.
|
|
|
|
// Like other languages, you can use `$this` to refer to the current instance.
|
|
|
|
|
|
|
|
class Counter {
|
|
|
|
private int $i = 0;
|
|
|
|
|
|
|
|
public function increment(): void {
|
|
|
|
$this->i += 1;
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
public function get(): int {
|
|
|
|
return $this->i;
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
2022-07-11 02:52:18 +00:00
|
|
|
}
|
2015-02-17 12:45:04 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
// Properties and Methods can be static (not requiring instantiation).
|
|
|
|
class Person {
|
|
|
|
public static function favoriteProgrammingLanguage(): string {
|
|
|
|
return "Hack";
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
2022-07-11 02:52:18 +00:00
|
|
|
}
|
2015-02-17 12:45:04 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
function demo_hack_classes(): void {
|
|
|
|
// Use `new` to instantiate a class.
|
|
|
|
$c1 = new Counter();
|
2015-02-17 12:45:04 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
// To call a static property or method, use `::`
|
|
|
|
$typical_person = tuple("Andrew", Person::favoriteProgrammingLanguage());
|
|
|
|
}
|
2015-02-17 12:45:04 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
// Abstract class can be defined, but not instantiated directly.
|
|
|
|
abstract class Machine {
|
|
|
|
public function openDoors(): void {
|
|
|
|
return;
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
2022-07-11 02:52:18 +00:00
|
|
|
public function closeDoors(): void {
|
|
|
|
return;
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
2022-07-11 02:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* INTERFACES
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
2022-07-11 22:44:32 +00:00
|
|
|
// A class can implement a set of requirements via an interface.
|
2022-07-11 02:52:18 +00:00
|
|
|
// An interface is a set of method declarations and constants.
|
|
|
|
|
|
|
|
interface Plane {
|
|
|
|
// A constant is a named value. Once defined, the value cannot be changed.
|
|
|
|
const MAX_SPEED = 300;
|
|
|
|
public function fly(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* TRAITS
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// A trait defines properties and method declarations.
|
|
|
|
// Traits are recommended when abstracting code for reuse.
|
|
|
|
// Traits are included in code via the `use` keyword.
|
|
|
|
|
|
|
|
trait Airplane {
|
2022-07-11 22:44:32 +00:00
|
|
|
// Introduce a class or interface requirement with the following syntax:
|
2022-07-11 02:52:18 +00:00
|
|
|
require extends Machine; // abstract class
|
|
|
|
require implements Plane; // interface
|
|
|
|
|
|
|
|
public function takeOff(): void {
|
|
|
|
$this->openDoors();
|
|
|
|
$this->closeDoors();
|
|
|
|
$this->fly();
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
2022-07-11 02:52:18 +00:00
|
|
|
}
|
2015-02-17 12:45:04 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
class Spaceship extends Machine implements Plane {
|
|
|
|
use Airplane;
|
2015-02-17 12:45:04 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
public function fly(): void {
|
|
|
|
// fly like the wind
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
2022-07-11 02:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ==================================
|
|
|
|
* KEEP READING!
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This is a simplified guide!
|
|
|
|
* There's much more to learn, including:
|
|
|
|
* - Asynchronous Operations: https://docs.hhvm.com/hack/asynchronous-operations/introduction
|
|
|
|
* - Reified Generics: https://docs.hhvm.com/hack/reified-generics/reified-generics
|
|
|
|
* - XHP: https://docs.hhvm.com/hack/XHP/setup
|
|
|
|
* - ... and more!
|
|
|
|
*/
|
2015-02-17 12:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## More Information
|
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
Visit the [Hack language reference](http://docs.hhvm.com/hack/) to learn more about the Hack language.
|
2015-02-17 12:45:04 +00:00
|
|
|
|
2022-07-11 02:52:18 +00:00
|
|
|
For more information on HHVM, including installation instructions, visit the [official HHVM website](http://hhvm.com/).
|