From 75f720fd34573d18b636a79effa2ab01181bbbff Mon Sep 17 00:00:00 2001 From: Stephen Holdaway Date: Wed, 18 Feb 2015 01:45:04 +1300 Subject: [hack/en] Add english Hack language page --- hack.html.markdown | 307 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 hack.html.markdown (limited to 'hack.html.markdown') diff --git a/hack.html.markdown b/hack.html.markdown new file mode 100644 index 00000000..dd163362 --- /dev/null +++ b/hack.html.markdown @@ -0,0 +1,307 @@ +--- +language: Hack +contributors: + - ["Stephen Holdaway", "https://github.com/stecman"] +filename: learnhack.hh +--- + +Hack is a superset of PHP that runs under a virtual machine called HHVM. Hack +is almost completely interoperable with existing PHP code and adds a bunch of +useful features from statically typed languages. + + +Only Hack-specific features are covered here. Details about PHP's syntax are +available in the [PHP article](php.html.markdown). + +```php +id = $id; + } +} + + +// Concise anonymous functions (lambdas) +$multiplier = 5; +array_map($y ==> $y * $multiplier, [1, 2, 3]); + + +// Generics +class Box +{ + protected T $data; + + public function __construct(T $data) { + $this->data = $data; + } + + public function getData(): T { + return $this->data; + } +} + +function openBox(Box $box) : int +{ + return $box->getData(); +} + + +// Shapes +// +// Hack adds the concept of shapes for defining struct-like arrays with a +// guaranteed, type-checked set of keys +type Point2D = shape('x' => int, 'y' => int); + +function distance(Point2D $a, Point2D $b) : float +{ + return sqrt(pow($b['x'] - $a['x'], 2) + pow($b['y'] - $a['y'], 2)); +} + +distance( + shape('x' => -1, 'y' => 5), + shape('x' => 2, 'y' => 50) +); + + +// Type aliasing +// +// Hack adds a bunch of type aliasing features for making complex types readable +newtype VectorArray = array>; + +// A tuple containing two integers +newtype Point = (int, int); + +function addPoints(Point $p1, Point $p2) : Point +{ + return tuple($p1[0] + $p2[0], $p1[1] + $p2[1]); +} + +addPoints( + tuple(1, 2), + tuple(5, 6) +); + + +// First-class enums +enum RoadType : int +{ + Road = 0; + Street = 1; + Avenue = 2; + Boulevard = 3; +} + +function getRoadType() : RoadType +{ + return RoadType::Avenue; +} + + +// Constructor argument promotion +// +// To avoid boilerplate property and constructor definitions that only set +// properties, Hack adds a concise syntax for defining properties and a +// constructor at the same time. +class ArgumentPromotion +{ + public function __construct(public string $name, + protected int $age, + private bool $isAwesome) {} +} + +class WithoutArugmentPromotion +{ + public string $name; + + protected int $age; + + private bool $isAwesome; + + public function __construct(string $name, int $age, bool $isAwesome) + { + $this->name = $name; + $this->age = $age; + $this->isAwesome = $isAwesome; + } +} + + +// Co-oprerative multi-tasking +// +// Two new keywords "async" and "await" can be used to perform mutli-tasking +// Note that this does not involve threads - it just allows transfer of control +async function cooperativePrint(int $start, int $end) : Awaitable +{ + for ($i = $start; $i <= $end; $i++) { + echo "$i "; + + // Give other tasks a chance to do something + await RescheduleWaitHandle::create(RescheduleWaitHandle::QUEUE_DEFAULT, 0); + } +} + +// This prints "1 4 7 2 5 8 3 6 9" +AwaitAllWaitHandle::fromArray([ + cooperativePrint(1, 3), + cooperativePrint(4, 6), + cooperativePrint(7, 9) +])->getWaitHandle()->join(); + + +// Attributes +// +// Attributes are a form of metadata for functions. Hack provides some +// special built-in attributes that introduce useful behaviour. + +// The __Memoize special attribute causes the result of a function to be cached +<<__Memoize>> +function doExpensiveTask() : ?string +{ + return file_get_contents('http://example.com'); +} + +// The function's body is only executed once here: +doExpensiveTask(); +doExpensiveTask(); + + +// The __ConsistentConstruct special attribute signals the Hack type checker to +// ensure that the signature of __construct is the same for all subclasses. +<<__ConsistentConstruct>> +class ConsistentFoo +{ + public function __construct(int $x, float $y) + { + // ... + } + + public function someMethod() + { + // ... + } +} + +class ConsistentBar extends ConsistentFoo +{ + public function __construct(int $x, float $y) + { + // Hack's type checker enforces that parent constructors are called + parent::__construct($x, $y); + + // ... + } + + // The __Override annotation is an optional signal for the Hack type + // checker to enforce that this method is overriding a method in a parent + // or trait. If not, this will error. + <<__Override>> + public function someMethod() + { + // ... + } +} + +class InvalidFooSubclass extends ConsistentFoo +{ + // Not matching the parent constructor will cause a type checker error: + // + // "This object is of type ConsistentBaz. It is incompatible with this object + // of type ConsistentFoo because some of their methods are incompatible" + // + public function __construct(float $x) + { + // ... + } + + // Using the __Override annotation on a non-overriden method will cause a + // type checker error: + // + // "InvalidFooSubclass::otherMethod() is marked as override; no non-private + // parent definition found or overridden parent is defined in non-> + public function otherMethod() + { + // ... + } +} + + +// Traits can implement interfaces (standard PHP does not support this) +interface KittenInterface +{ + public function play() : void; +} + +trait CatTrait implements KittenInterface +{ + public function play() : void + { + // ... + } +} + +class Samuel +{ + use CatTrait; +} + + +$cat = new Samuel(); +$cat instanceof KittenInterface === true; // True + +``` + +## More Information + +Visit the [Hack language reference](http://docs.hhvm.com/manual/en/hacklangref.php) +for detailed explainations of the features Hack adds to PHP, or the [official Hack website](http://hacklang.org/) +for more general information. + +Visit the [official HHVM website](http://hhvm.com/) for HHVM installation instructions. + +Visit [Hack's unsupported PHP features article](http://docs.hhvm.com/manual/en/hack.unsupported.php).] +for details on the backwards incompatibility between Hack and PHP. \ No newline at end of file -- cgit v1.2.3 From 177b51a3317cf1e6b55ec3e9439bc6b866440e41 Mon Sep 17 00:00:00 2001 From: Stephen Holdaway Date: Wed, 18 Feb 2015 19:23:58 +1300 Subject: [hack/en] Fix link to other learnxinyminutes article I had assumed a relative link to another markdown file in this project would work, but it renders as: http://learnxinyminutes.com/docs/hack/php.html.markdown instead of the desired: http://learnxinyminutes.com/docs/php/ I've replaced this relative link with an absolute link to where the target page is located on the published site. This commit also fixes a typo in markdown syntax at the end of the document. --- hack.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'hack.html.markdown') diff --git a/hack.html.markdown b/hack.html.markdown index dd163362..632fc705 100644 --- a/hack.html.markdown +++ b/hack.html.markdown @@ -11,7 +11,7 @@ useful features from statically typed languages. Only Hack-specific features are covered here. Details about PHP's syntax are -available in the [PHP article](php.html.markdown). +available in the [PHP article](http://learnxinyminutes.com/docs/php/) on this site. ```php Date: Sun, 4 Oct 2015 11:55:16 -0300 Subject: [hack/en] Fixed some typos --- hack.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'hack.html.markdown') diff --git a/hack.html.markdown b/hack.html.markdown index 632fc705..b9730dc0 100644 --- a/hack.html.markdown +++ b/hack.html.markdown @@ -2,6 +2,7 @@ language: Hack contributors: - ["Stephen Holdaway", "https://github.com/stecman"] + - ["David Lima", "https://github.com/davelima"] filename: learnhack.hh --- @@ -152,7 +153,7 @@ class ArgumentPromotion private bool $isAwesome) {} } -class WithoutArugmentPromotion +class WithoutArgumentPromotion { public string $name; @@ -169,9 +170,9 @@ class WithoutArugmentPromotion } -// Co-oprerative multi-tasking +// Co-operative multi-tasking // -// Two new keywords "async" and "await" can be used to perform mutli-tasking +// Two new keywords "async" and "await" can be used to perform multi-tasking // Note that this does not involve threads - it just allows transfer of control async function cooperativePrint(int $start, int $end) : Awaitable { -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- hack.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'hack.html.markdown') diff --git a/hack.html.markdown b/hack.html.markdown index b9730dc0..b3d19f8e 100644 --- a/hack.html.markdown +++ b/hack.html.markdown @@ -51,7 +51,7 @@ function identity(?string $stringOrNull) : ?string class TypeHintedProperties { public ?string $name; - + protected int $id; private float $score = 100.0; @@ -91,7 +91,7 @@ function openBox(Box $box) : int // Shapes -// +// // Hack adds the concept of shapes for defining struct-like arrays with a // guaranteed, type-checked set of keys type Point2D = shape('x' => int, 'y' => int); @@ -108,7 +108,7 @@ distance( // Type aliasing -// +// // Hack adds a bunch of type aliasing features for making complex types readable newtype VectorArray = array>; @@ -142,7 +142,7 @@ function getRoadType() : RoadType // Constructor argument promotion -// +// // To avoid boilerplate property and constructor definitions that only set // properties, Hack adds a concise syntax for defining properties and a // constructor at the same time. @@ -171,12 +171,12 @@ class WithoutArgumentPromotion // Co-operative multi-tasking -// +// // Two new keywords "async" and "await" can be used to perform multi-tasking // Note that this does not involve threads - it just allows transfer of control async function cooperativePrint(int $start, int $end) : Awaitable { - for ($i = $start; $i <= $end; $i++) { + for ($i = $start; $i <= $end; $i++) { echo "$i "; // Give other tasks a chance to do something @@ -193,9 +193,9 @@ AwaitAllWaitHandle::fromArray([ // Attributes -// +// // Attributes are a form of metadata for functions. Hack provides some -// special built-in attributes that introduce useful behaviour. +// special built-in attributes that introduce useful behaviour. // The __Memoize special attribute causes the result of a function to be cached <<__Memoize>> @@ -248,7 +248,7 @@ class ConsistentBar extends ConsistentFoo class InvalidFooSubclass extends ConsistentFoo { // Not matching the parent constructor will cause a type checker error: - // + // // "This object is of type ConsistentBaz. It is incompatible with this object // of type ConsistentFoo because some of their methods are incompatible" // @@ -259,7 +259,7 @@ class InvalidFooSubclass extends ConsistentFoo // Using the __Override annotation on a non-overriden method will cause a // type checker error: - // + // // "InvalidFooSubclass::otherMethod() is marked as override; no non-private // parent definition found or overridden parent is defined in non-