Incorporate micro-feedback

* Update documentation for using contains() and contains_key() with dict (previously incorrect)
* Prefer to use HH\Asio\curl_exec() over \file_get_contents
* Add example for enum class (enum of any type)
* Remove incorrect description of `use` keyword in traits
* Fix and improve various explanations in comments
This commit is contained in:
Andrew 2022-07-11 18:44:32 -04:00 committed by GitHub
parent 52cec041d0
commit dd5c81e40d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,7 +64,7 @@ namespace LearnHackinYMinutes {
function demo_hack_types(): void { function demo_hack_types(): void {
// Hack has five primitive types: bool, int, float, string, and null // Hack has five primitive types: bool, int, float, string, and null.
$is_helpful = true; // bool $is_helpful = true; // bool
$int_value = 10; // int $int_value = 10; // int
$precise_value = 2.0; // float $precise_value = 2.0; // float
@ -106,6 +106,12 @@ namespace LearnHackinYMinutes {
Delete = 'D'; Delete = 'D';
} }
// In contrast, an enum class can be of any value type!
enum class Random : mixed {
int X = 42;
string S = 'foo';
}
/* ================================== /* ==================================
* HACK ARRAYS * HACK ARRAYS
* ================================== * ==================================
@ -140,8 +146,8 @@ namespace LearnHackinYMinutes {
* ================================== * ==================================
*/ */
// The Hack Standard Library is a set of functions and classes for the Hack language // The Hack Standard Library is a set of functions and classes for the Hack language.
// Imports are ideally at the top of your file but are placed here for instruction purposes // Namespace use declarations are ideally at the top of your file but are placed here for instruction purposes.
use namespace HH\Lib\C; // the `C` library operates on containers (like Hack Arrays) use namespace HH\Lib\C; // the `C` library operates on containers (like Hack Arrays)
use namespace HH\Lib\Str; // The `Str` library operates on strings use namespace HH\Lib\Str; // The `Str` library operates on strings
@ -154,7 +160,8 @@ namespace LearnHackinYMinutes {
C\contains($letters, 'c'); // checks for a value; returns 'true' C\contains($letters, 'c'); // checks for a value; returns 'true'
C\contains($colors, 'purple'); // checks for a value; returns 'false' C\contains($colors, 'purple'); // checks for a value; returns 'false'
C\contains($alphabet, 'a'); // checks for a value; returns 'true' C\contains_key($alphabet, 'a'); // checks for a key; returns 'true'
C\contains($alphabet, 'd'); // checks for a value; returns 'false'
Str\length("foo"); // returns `3` Str\length("foo"); // returns `3`
Str\join(vec['foo', 'bar', 'baz'], '!'); // returns `foo!bar!baz` Str\join(vec['foo', 'bar', 'baz'], '!'); // returns `foo!bar!baz`
@ -184,7 +191,9 @@ namespace LearnHackinYMinutes {
// Functions are defined globally. // Functions are defined globally.
// When a function is defined in a class, we refer to the function as a method. // When a function is defined in a class, we refer to the function as a method.
// Functions have return types (here: `int`) and must return a type or nothing (`void`). // 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.
function add_one(int $x): int { function add_one(int $x): int {
return $x + 1; return $x + 1;
} }
@ -217,9 +226,9 @@ namespace LearnHackinYMinutes {
// As another example, `__Memoize` caches the result of a function. // As another example, `__Memoize` caches the result of a function.
<<__Memoize>> <<__Memoize>>
function do_expensive_task(): ?string { async function do_expensive_task(): Awaitable<string> {
// return file_get_contents('http://hacklang.org'); $site_contents = await \HH\Asio\curl_exec("http://hacklang.org");
return "dynamic string with contents from hacklang.org"; return $site_contents;
} }
/* ================================== /* ==================================
@ -319,7 +328,7 @@ namespace LearnHackinYMinutes {
* ================================== * ==================================
*/ */
// A class can implement a set of capabilities via an interface. // A class can implement a set of requirements via an interface.
// An interface is a set of method declarations and constants. // An interface is a set of method declarations and constants.
interface Plane { interface Plane {
@ -336,10 +345,9 @@ namespace LearnHackinYMinutes {
// A trait defines properties and method declarations. // A trait defines properties and method declarations.
// Traits are recommended when abstracting code for reuse. // Traits are recommended when abstracting code for reuse.
// Traits are included in code via the `use` keyword. // Traits are included in code via the `use` keyword.
// `use` allows for other includes, like namespaces, classes, and functions (and more)!
trait Airplane { trait Airplane {
// Like other languages, classes are extended, and interfaces are implemented. // Introduce a class or interface requirement with the following syntax:
require extends Machine; // abstract class require extends Machine; // abstract class
require implements Plane; // interface require implements Plane; // interface