language
stringlengths
0
24
filename
stringlengths
9
214
code
stringlengths
99
9.93M
Hack
hhvm/hphp/hack/test/extracted_from_manual/04-statements/08-switch-01.hack
// @generated by hh_manual from manual/hack/04-statements/08-switch.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $v = 100; switch ($v) { case 20: // ... break; case 10: // ... break; case 30: // ... break; default: // ... break; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/04-statements/08-switch-02.hack
// @generated by hh_manual from manual/hack/04-statements/08-switch.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $v = 10; switch ($v) { case 10: // ... // FALLTHROUGH case 30: // ... // Handle 10 or 30 break; default: // ... break; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/04-statements/08-switch-03.hack
// @generated by hh_manual from manual/hack/04-statements/08-switch.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $v = 30; switch ($v) { case 30.0: // <===== this case matches with 30 // ... break; default: // ... break; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/04-statements/09-try-01.hack
// @generated by hh_manual from manual/hack/04-statements/09-try.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class MyException extends Exception {} function demo(): void { throw new MyException(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/04-statements/09-try-02.hack
// @generated by hh_manual from manual/hack/04-statements/09-try.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function do_it(int $x, int $y): void { try { $result = $x / $y; echo "\$result = $result\n"; // ... } catch (DivisionByZeroException $ex) { echo "Caught a DivisionByZeroException\n"; // ... } catch (Exception $ex) { echo "Caught an Exception\n"; // ... } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/04-statements/09-try-03.hack
// @generated by hh_manual from manual/hack/04-statements/09-try.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class DeviceException extends Exception { /*...*/ } class DiskException extends DeviceException { /*...*/ } class RemovableDiskException extends DiskException { /*...*/ } class FloppyDiskException extends RemovableDiskException { /*...*/ } function process(): void { throw new DeviceException(); } <<__EntryPoint>> function main(): void { try { process(); // call a function that might generate a disk-related exception } catch (FloppyDiskException $fde) { echo "In handler for FloppyDiskException\n"; // ... } catch (RemovableDiskException $rde) { echo "In handler for RemovableDiskException\n"; // ... } catch (DiskException $de) { echo "In handler for DiskException\n"; // ... } catch (DeviceException $dve) { echo "In handler for DeviceException\n"; // ... } finally { echo "In finally block\n"; // perform some cleanup } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/04-statements/10-use-use.hack
// @generated by hh_manual from manual/hack/04-statements/10-use.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ namespace UseNS { const int CON = 100; function f(): void { echo "In function ".__FUNCTION__."\n"; } class C { public function f(): void { echo "In method ".__METHOD__."\n"; } } class D {} class E {} } namespace Hack\UserDocumentation\Statements\use\Examples\XXX { const int CON2 = 500; function f(): void { echo "In function ".__FUNCTION__."\n"; } } namespace Hack\UserDocumentation\Statements\use\Examples\test { use const UseNS\CON; use function UseNS\f; //use function Hack\UserDocumentation\Statements\use\Examples\XXX\f; // Error: name f already declared use type UseNS\C; use type UseNS\{D, E}; use namespace Hack\UserDocumentation\Statements\use\Examples\XXX; <<__EntryPoint>> function main(): void { // access const CON by fully qualified and abbreviated names echo "CON = ".\UseNS\CON."\n"; echo "CON = ".CON."\n"; // access function f by fully qualified and abbreviated names \UseNS\f(); f(); // access type C by fully qualified and abbreviated names $c = new \UseNS\C(); $c->f(); $c = new C(); $c->f(); // access type D by fully qualified and abbreviated names $d = new \UseNS\D(); $d = new D(); // access name f by fully qualified and abbreviated names \Hack\UserDocumentation\Statements\use\Examples\XXX\f(); XXX\f(); // access name CON2 by fully qualified and abbreviated names echo "XXX\CON2 = ". \Hack\UserDocumentation\Statements\use\Examples\XXX\CON2. "\n"; echo "XXX\\CON2 = ".XXX\CON2."\n"; } } namespace { use namespace Hack\UserDocumentation\Statements\use\Examples\XXX; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/04-statements/12-while-01.hack
// @generated by hh_manual from manual/hack/04-statements/12-while.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $i = 1; while ($i <= 10) { echo "$i\t".($i * $i)."\n"; // output a table of squares ++$i; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/01-introduction-01.hack
// @generated by hh_manual from manual/hack/05-functions/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function add_one(int $x): int { return $x + 1; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/01-introduction-02.hack
// @generated by hh_manual from manual/hack/05-functions/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function add_value(int $x, int $y = 1): int { return $x + $y; }
hhvm/hphp/hack/test/extracted_from_manual/05-functions/01-introduction-03.hack_error
// @generated by hh_manual from manual/hack/05-functions/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function add_value_bad(int $x = 1, int $y): int { return $x + $y; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/01-introduction-04.hack
// @generated by hh_manual from manual/hack/05-functions/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function apply_func(int $v, (function(int): int) $f): int { return $f($v); } function usage_example(): void { $x = apply_func(0, $x ==> $x + 1); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/01-introduction-05.hack
// @generated by hh_manual from manual/hack/05-functions/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_variadic_fun((function(int...): void) $f): void { $f(1, 2, 3); $args = vec[1, 2, 3]; $f(0, ...$args); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/01-introduction-sumints.hack
// @generated by hh_manual from manual/hack/05-functions/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function sum_ints(int $val, int ...$vals): int { $result = $val; foreach ($vals as $v) { $result += $v; } return $result; } async function example_snippet_wrapper1(): Awaitable<void> { // Passing positional arguments. sum_ints(1, 2, 3); // You can also pass a collection into a variadic parameter. $args = vec[1, 2, 3]; sum_ints(0, ...$args); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/02-anonymous-functions-01.hack
// @generated by hh_manual from manual/hack/05-functions/02-anonymous-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $f = $x ==> $x + 1; $two = $f(1); // result of 2 }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/02-anonymous-functions-02.hack
// @generated by hh_manual from manual/hack/05-functions/02-anonymous-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $f = ($x, $y) ==> $x + $y; $three = $f(1, 2); // result of 3 }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/02-anonymous-functions-03.hack
// @generated by hh_manual from manual/hack/05-functions/02-anonymous-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $x = 5; $f = $x ==> $x + 1; $six = $f($x); // pass by value echo($six); // result of 6 echo("\n"); echo($x); // $x is unchanged; result of 5 }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/02-anonymous-functions-04.hack
// @generated by hh_manual from manual/hack/05-functions/02-anonymous-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $f = (int $x): int ==> $x + 1; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/02-anonymous-functions-05.hack
// @generated by hh_manual from manual/hack/05-functions/02-anonymous-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $f1 = $x ==> $x + 1; $f2 = $x ==> { return $x + 1; }; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/02-anonymous-functions-06.hack
// @generated by hh_manual from manual/hack/05-functions/02-anonymous-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $f = function($x) { return $x + 1; }; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/02-anonymous-functions-07.hack
// @generated by hh_manual from manual/hack/05-functions/02-anonymous-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $y = 1; $f = function($x) use($y) { return $x + $y; }; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/02-anonymous-functions-08.hack
// @generated by hh_manual from manual/hack/05-functions/02-anonymous-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $y = 1; $f = function(int $x): int use($y) { return $x + $y; }; }
hhvm/hphp/hack/test/extracted_from_manual/05-functions/03-type-enforcement-01.hack_error
// @generated by hh_manual from manual/hack/05-functions/03-type-enforcement.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_int(int $_): void {} function check_parameter(): void { takes_int("not an int"); // runtime error. } function check_return_value(): int { return "not an int"; // runtime error. }
hhvm/hphp/hack/test/extracted_from_manual/05-functions/05-format-strings-01.hack_error
// @generated by hh_manual from manual/hack/05-functions/05-format-strings.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { // Correct. Str\format("First: %d, second: %s", 1, "foo"); // Typechecker error: Too few arguments for format string. Str\format("First: %d, second: %s", 1); }
hhvm/hphp/hack/test/extracted_from_manual/05-functions/05-format-strings-02.hack_error
// @generated by hh_manual from manual/hack/05-functions/05-format-strings.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $string = "Number is: %d"; // Typechecker error: Only string literals are allowed here. Str\format($string, 1); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/05-format-strings-03.hack
// @generated by hh_manual from manual/hack/05-functions/05-format-strings.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_format_string( \HH\FormatString<\PlainSprintf> $format, mixed ...$args ): void {} function use_it(): void { takes_format_string("First: %d, second: %s", 1, "foo"); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/20-inout-parameters-01.hack
// @generated by hh_manual from manual/hack/05-functions/20-inout-parameters.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_inout(inout int $x): void { $x = 1; } function call_it(): void { $num = 0; takes_inout(inout $num); // $num is now 1. }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/20-inout-parameters-02.hack
// @generated by hh_manual from manual/hack/05-functions/20-inout-parameters.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_inout(inout int $x): void { $x = 1; throw new Exception(); } <<__EntryPoint>> function call_it(): void { $num = 0; try { takes_inout(inout $num); } catch (Exception $_) { } // $num is still 0. }
Hack
hhvm/hphp/hack/test/extracted_from_manual/05-functions/20-inout-parameters-03.hack
// @generated by hh_manual from manual/hack/05-functions/20-inout-parameters.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function set_to_value(inout int $item, int $value): void { $item = $value; } function use_it(): void { $items = vec[10, 11, 12]; $index = 1; set_to_value(inout $items[$index], 42); // $items is now vec[10, 42, 12]. }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/01-introduction-01.hack
// @generated by hh_manual from manual/hack/06-classes/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Counter { private int $i = 0; public function increment(): void { $this->i += 1; } public function get(): int { return $this->i; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/03-methods-person.hack
// @generated by hh_manual from manual/hack/06-classes/03-methods.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Person { public string $name = "anonymous"; public function greeting(): string { return "Hi, my name is ".$this->name; } } async function example_snippet_wrapper1(): Awaitable<void> { $p = new Person(); echo $p->greeting(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/03-methods-person2.hack
// @generated by hh_manual from manual/hack/06-classes/03-methods.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Person { public static function typicalGreeting(): string { return "Hello"; } } async function example_snippet_wrapper1(): Awaitable<void> { echo Person::typicalGreeting(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/05-properties-01.hack
// @generated by hh_manual from manual/hack/06-classes/05-properties.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class HasDefaultValue { public int $i = 0; } class SetInConstructor { public int $i; public function __construct() { $this->i = 0; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/05-properties-02.hack
// @generated by hh_manual from manual/hack/06-classes/05-properties.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class MyExample { public mixed $m; public ?string $s; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/05-properties-example.hack
// @generated by hh_manual from manual/hack/06-classes/05-properties.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Example { public static int $val = 0; } async function example_snippet_wrapper1(): Awaitable<void> { Example::$val; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/05-properties-funbox.hack
// @generated by hh_manual from manual/hack/06-classes/05-properties.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class FunctionBox { public function __construct(public (function(): void) $value) {} } async function example_snippet_wrapper1(): Awaitable<void> { $b = new FunctionBox(() ==> { echo "hello"; }); ($b->value)(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/05-properties-intbox.hack
// @generated by hh_manual from manual/hack/06-classes/05-properties.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class IntBox { public int $value = 0; } async function example_snippet_wrapper1(): Awaitable<void> { $b = new IntBox(); $b->value = 42; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/05-properties-intbox_prop.hack
// @generated by hh_manual from manual/hack/06-classes/05-properties.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class IntBox { public int $value = 0; public function value(): int { return $this->value; } } async function example_snippet_wrapper1(): Awaitable<void> { $b = new IntBox(); $b->value(); // method call $b->value; // property access }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-01.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class IntBox { public function __construct(protected int $value) {} public function get(): int { return $this->value; } } class MutableIntBox extends IntBox { public function set(int $value): void { $this->value = $value; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-02.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class IntBox { public function __construct(protected int $value) {} public function get(): int { return $this->value; } } class IncrementedIntBox extends IntBox { <<__Override>> public function get(): int { return $this->value + 1; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-03.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class NumBox { public function __construct(protected num $value) {} protected function get(): num { return $this->value; } } class FloatBox extends NumBox { <<__Override>> public function get(): float { return (float)$this->value; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-04.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class User { // This constructor takes one argument. public function __construct(protected string $name) {} } class Player extends User { // But this constructor takes two arguments. <<__Override>> public function __construct(protected int $score, string $name) { parent::__construct($name); } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-05.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class IntBox { public function __construct(protected int $value) {} public function get(): int { return $this->value; } } class IncrementedIntBox extends IntBox { <<__Override>> public function get(): int { return parent::get() + 1; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-06.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class MyParent { public static function foo(): int { return 0; } } class MyChild extends MyParent { <<__Override>> public static function foo(): int { return parent::foo() + 1; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-07.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class Animal { public abstract function greet(): string; } class Dog extends Animal { <<__Override>> public function greet(): string { return "woof!"; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-08.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ final class Dog { public function greet(): string { return "woof!"; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-09.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract final class Example { public static function callMe(int $i): int { return static::helper($i); } private static function helper(int $i): int { return $i + 1; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/06-inheritance-10.hack
// @generated by hh_manual from manual/hack/06-classes/06-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class IntBox { public function __construct(protected int $value) {} final public function get(): int { return $this->value; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/16-constructors-01.hack
// @generated by hh_manual from manual/hack/06-classes/16-constructors.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Point { private static int $pointCount = 0; // static property with initializer private float $x; // instance property private float $y; // instance property public function __construct(num $x = 0, num $y = 0) { // instance method $this->x = (float)$x; // access instance property $this->y = (float)$y; // access instance property ++Point::$pointCount; // include new Point in Point count } } function demo(): void { $p1 = new Point(2.3); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/16-constructors-02.hack
// @generated by hh_manual from manual/hack/06-classes/16-constructors.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ final class User { private int $id; private string $name; public function __construct( int $id, string $name, ) { $this->id = $id; $this->name = $name; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/16-constructors-03.hack
// @generated by hh_manual from manual/hack/06-classes/16-constructors.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ final class User { public function __construct( private int $id, private string $name, ) {} }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/16-constructors-04.hack
// @generated by hh_manual from manual/hack/06-classes/16-constructors.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ final class User { private static dict<int, User> $allUsers = dict[]; private int $age; public function __construct( private int $id, private string $name, // Promoted parameters can be combined with regular non-promoted parameters. int $birthday_year, ) { $this->age = \date('Y') - $birthday_year; // The constructor parameter promotion assignments are done before the code // inside the constructor is run, so we can use $this->id here. self::$allUsers[$this->id] = $this; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/18-constants-01.hack
// @generated by hh_manual from manual/hack/06-classes/18-constants.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Automobile { const DEFAULT_COLOR = "white"; // ... } <<__EntryPoint>> function main(): void { $col = Automobile::DEFAULT_COLOR; // or: $col = self::DEFAULT_COLOR; echo "\$col = $col\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/19-type-constants-01.hack
// @generated by hh_manual from manual/hack/06-classes/19-type-constants.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class CBase { abstract const type T; // ... } class CString extends CBase { const type T = string; // ... }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/21-object-disposal-01.hack
// @generated by hh_manual from manual/hack/06-classes/21-object-disposal.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class TextFile implements \IDisposable { private ?int $fileHandle = null; private bool $openFlag = false; private string $fileName; private string $openMode; public function __construct(string $fileName, string $openMode) { $this->fileHandle = 55; // open file somehow and store handle $this->openFlag = true; // file is open $this->fileName = $fileName; $this->openMode = $openMode; } public function close(): void { if ($this->openFlag === false) { return; } // ... somehow close the file $this->fileHandle = null; $this->openFlag = false; echo "Closed file $this->fileName\n"; } public function __toString(): string { return 'fileName: '. $this->fileName. ', openMode: '. $this->openMode. ', fileHandle: '. (($this->fileHandle === null) ? "null" : $this->fileHandle). ', openFlag: '. (($this->openFlag) ? "True" : "False"); } public function __dispose(): void { echo "Inside __dispose\n"; $this->close(); } <<__ReturnDisposable>> public static function open_TextFile( string $fileName, string $openMode, ): TextFile { return new TextFile($fileName, $openMode); } public function is_same_TextFile(<<__AcceptDisposable>> TextFile $t): bool { return $this->fileHandle === $t->fileHandle; } // other methods, such as read and write } <<__EntryPoint>> function main(): void { using ($f1 = new TextFile("file1.txt", "rw")) { // echo "\$f1 is >" . $f1 . "<\n"; // usage not permitted echo "\$f1 is >".$f1->__toString()."<\n"; // work with the file $f1->close(); // close explicitly $f1->close(); // try to close again } // dispose called here using ($f2 = new TextFile("file2.txt", "rw")) { echo "\$f2 is >".$f2->__toString()."<\n"; // work with the file // no explicit close } // dispose called here using ($f3 = TextFile::open_TextFile("file3.txt", "rw")) { echo "\$f3 is >".$f3->__toString()."<\n"; // work with the file // no explicit close } // dispose called here using $f4 = TextFile::open_TextFile("file4.txt", "rw"); echo "\$f4 is >".$f4->__toString()."<\n"; using $f5 = new TextFile("file5.txt", "rw"); echo "\$f5 is >".$f5->__toString()."<\n"; // work with both files // no explicit close } // dispose called here for both $f4 and $f5
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/21-object-disposal-02.hack
// @generated by hh_manual from manual/hack/06-classes/21-object-disposal.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Example implements IAsyncDisposable { public async function __disposeAsync(): Awaitable<void> { // Cleanup here. } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/31-type-constants-revisited-01.hack
// @generated by hh_manual from manual/hack/06-classes/31-type-constants-revisited.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class User { public function __construct(private int $id) {} public function getID(): int { return $this->id; } } trait UserTrait { require extends User; } interface IUser { require extends User; } class AppUser extends User implements IUser { use UserTrait; } <<__EntryPoint>> function run(): void { $au = new AppUser(-1); \var_dump($au->getID()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/31-type-constants-revisited-02.hack
// @generated by hh_manual from manual/hack/06-classes/31-type-constants-revisited.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class User { abstract const type T as arraykey; public function __construct(private this::T $id) {} public function getID(): this::T { return $this->id; } } trait UserTrait { require extends User; } interface IUser { require extends User; } // We know that AppUser will only have int ids class AppUser extends User implements IUser { const type T = int; use UserTrait; } class WebUser extends User implements IUser { const type T = string; use UserTrait; } class OtherUser extends User implements IUser { const type T = arraykey; use UserTrait; } <<__EntryPoint>> function run(): void { $au = new AppUser(-1); \var_dump($au->getID()); $wu = new WebUser('-1'); \var_dump($wu->getID()); $ou1 = new OtherUser(-1); \var_dump($ou1->getID()); $ou2 = new OtherUser('-1'); \var_dump($ou2->getID()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/31-type-constants-revisited-03.hack
// @generated by hh_manual from manual/hack/06-classes/31-type-constants-revisited.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class Base { abstract const type T; protected this::T $value; } class Stringy extends Base { const type T = string; public function __construct() { // inherits $value in Base which is now setting T as a string $this->value = "Hi"; } public function getString(): string { return $this->value; // property of type string } } class Inty extends Base { const type T = int; public function __construct() { // inherits $value in Base which is now setting T as an int $this->value = 4; } public function getInt(): int { return $this->value; // property of type int } } <<__EntryPoint>> function run(): void { $s = new Stringy(); $i = new Inty(); \var_dump($s->getString()); \var_dump($i->getInt()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/31-type-constants-revisited-04.hack
// @generated by hh_manual from manual/hack/06-classes/31-type-constants-revisited.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class UserTC { abstract const type Ttc as arraykey; public function __construct(private this::Ttc $id) {} public function getID(): this::Ttc { return $this->id; } } class AppUserTC extends UserTC { const type Ttc = int; } function get_id_from_userTC(AppUserTC $uc): AppUserTC::Ttc { return $uc->getID(); } <<__EntryPoint>> function run(): void { $autc = new AppUserTC(10); \var_dump(get_id_from_userTC($autc)); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/31-type-constants-revisited-05.hack
// @generated by hh_manual from manual/hack/06-classes/31-type-constants-revisited.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class Box { abstract const type T; public function __construct(private this::T $value) {} public function get(): this::T { return $this->value; } public function set(this::T $val): this { $this->value = $val; return $this; } } class IntBox extends Box { const type T = int; } <<__EntryPoint>> function run(): void { $ibox = new IntBox(10); \var_dump($ibox); $ibox->set(123); \var_dump($ibox); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/06-classes/33-methods-with-predefined-semantics-01.hack
// @generated by hh_manual from manual/hack/06-classes/33-methods-with-predefined-semantics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Example implements IDisposable { public function __dispose(): void {} }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/02-implementing-an-interface-01.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/02-implementing-an-interface.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ interface MyCollection { const MAX_NUMBER_ITEMS = 1000; public function put(int $item): void; public function get(): int; } class MyList implements MyCollection { public function put(int $item): void { /* implement method */ } public function get(): int { /* implement method */ return 0; } // ... } class MyQueue implements MyCollection { public function put(int $item): void { /* implement method */ } public function get(): int { /* implement method */ return 0; } // ... } function process_collection(MyCollection $p1): void { /* can process any object whose class implements MyCollection */ $p1->put(123); } <<__EntryPoint>> function main(): void { process_collection(new MyList()); process_collection(new MyQueue()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/03-using-a-trait-01.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/03-using-a-trait.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ trait T { public int $x = 0; public function return_even() : int { invariant($this->x % 2 == 0, 'error, not even\n'); $this->x = $this->x + 2; return $this->x; } } class C1 { use T; public function foo() : void { echo "C1: " . $this->return_even() . "\n"; } } class C2 { use T; public function bar() : void { echo "C2: " . $this->return_even() . "\n"; } } <<__EntryPoint>> function main() : void { (new C1()) -> foo(); (new C2()) -> bar(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/03-using-a-trait-02.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/03-using-a-trait.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ trait T1 { public int $x = 0; public function return_even() : int { invariant($this->x % 2 == 0, 'error, not even\n'); $this->x = $this->x + 2; return $this->x; } } trait T2 { use T1; public function return_odd() : int { return $this->return_even() + 1; } } trait T3 { public static function is_odd(int $x) : bool { if ($x % 2 == 1) { return true; } else { return false; } } } class C { use T2; use T3; public function foo() : void { echo static::is_odd($this->return_odd()) . "\n"; } } <<__EntryPoint>> function main() : void { (new C()) -> foo(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/03-using-a-trait-03.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/03-using-a-trait.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ trait T1 { public static int $x = 0; public static function even() : void { invariant(static::$x % 2 == 0, 'error, not even\n'); static::$x = static::$x + 2; } } trait T2 { public static int $x = 0; public static function inc() : void { static::$x = static::$x + 1; } } class C { use T1; use T2; public static function foo() : void { static::inc(); static::even(); } } <<__EntryPoint>> function main() : void { try { C::foo(); } catch (\Exception $ex) { echo "Caught an exception\n"; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/03-using-a-trait-04.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/03-using-a-trait.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<file:__EnableUnstableFeatures('method_trait_diamond')>> trait T { public function foo(): void { echo "I am T"; } } trait T1 { use T; } trait T2 { use T; } <<__EnableMethodTraitDiamond>> class C { use T1, T2; } <<__EntryPoint>> function main() : void { (new C())->foo(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/03-using-a-trait-05.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/03-using-a-trait.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ trait T { const FOO = 'trait'; } class B { const FOO = 'parent'; } class A extends B { use T; } <<__EntryPoint>> function main() : void { \var_dump(A::FOO); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/03-using-a-trait-06.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/03-using-a-trait.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ trait T1 { const FOO = 'one'; } trait T2 { const FOO = 'two'; } class A { use T1, T2; } <<__EntryPoint>> function main() : void { \var_dump(A::FOO); }
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/03-using-a-trait-07.hack_error
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/03-using-a-trait.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ trait T { const FOO = 'trait'; } interface I { const FOO = 'interface'; } class A implements I { use T; } <<__EntryPoint>> function main() : void { \var_dump(A::FOO); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/03-using-a-trait-08.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/03-using-a-trait.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ interface I1 { const FOO = 'one'; } trait T implements I1 {} interface I { const FOO = 'two'; } class A implements I { use T; } <<__EntryPoint>> function main() : void { \var_dump(A::FOO); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/04-trait-and-interface-requirements-01.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/04-trait-and-interface-requirements.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class Machine { public function openDoors(): void { return; } public function closeDoors(): void { return; } } interface Fliers { public function fly(): bool; } trait Plane { require extends Machine; require implements Fliers; public function takeOff(): bool { $this->openDoors(); $this->closeDoors(); return $this->fly(); } } class AirBus extends Machine implements Fliers { use Plane; public function fly(): bool { return true; } } <<__EntryPoint>> function run(): void { $ab = new AirBus(); \var_dump($ab); \var_dump($ab->takeOff()); }
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/04-trait-and-interface-requirements-02.hack_error
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/04-trait-and-interface-requirements.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class Machine { public function openDoors(): void { return; } public function closeDoors(): void { return; } } interface Fliers { public function fly(): bool; } trait Plane { require extends Machine; require implements Fliers; public function takeOff(): bool { $this->openDoors(); $this->closeDoors(); return $this->fly(); } } // Having this will not only cause a typechecker error, but also cause a fatal // error in HHVM since we did not meet the trait requirement. class Paper implements Fliers { use Plane; public function fly(): bool { return false; } } <<__EntryPoint>> function run(): void { // This code will not run in HHVM because of the problem mentioned above. $p = new Paper(); \var_dump($p); \var_dump($p->takeOff()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/04-trait-and-interface-requirements-03.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/04-trait-and-interface-requirements.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<file:__EnableUnstableFeatures('require_class')>> trait T { require class C; public function foo(): void { $this->bar(); } } final class C { use T; public function bar(): void {} }
Hack
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/04-trait-and-interface-requirements-04.hack
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/04-trait-and-interface-requirements.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class Machine { public function openDoors(): void { return; } public function closeDoors(): void { return; } } interface Fliers { require extends Machine; public function fly(): bool; } class AirBus extends Machine implements Fliers { public function takeOff(): bool { $this->openDoors(); $this->closeDoors(); return $this->fly(); } public function fly(): bool { return true; } } <<__EntryPoint>> function run(): void { $ab = new AirBus(); \var_dump($ab); \var_dump($ab->takeOff()); }
hhvm/hphp/hack/test/extracted_from_manual/07-traits-and-interfaces/04-trait-and-interface-requirements-05.hack_error
// @generated by hh_manual from manual/hack/07-traits-and-interfaces/04-trait-and-interface-requirements.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract class Machine { public function openDoors(): void { return; } public function closeDoors(): void { return; } } interface Fliers { require extends Machine; public function fly(): bool; } // Having this will not only cause a typechecker error, but also cause a fatal // error in HHVM since we did not meet the interface requirement (extending // Machine). class Paper implements Fliers { public function fly(): bool { return false; } } <<__EntryPoint>> function run(): void { // This code will actually not run in HHVM because of the fatal mentioned // above. $p = new Paper(); \var_dump($p); \var_dump($p->takeOff()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/01-introduction-01.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $v = vec[2, 1, 2]; $k = keyset[2, 1]; $d = dict['a' => 1, 'b' => 3]; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/01-introduction-02.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { // The C namespace contains generic functions that are relevant to // all array and collection types. C\count(vec[]); // 0 C\is_empty(keyset[]); // true // The Vec, Keyset and Dict namespaces group functions according // to their return type. Vec\keys(dict['x' => 1]); // vec['x'] Keyset\keys(dict['x' => 1]); // keyset['x'] Vec\map(keyset[1, 2], $x ==> $x + 1); // vec[2, 3] }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/05-vec-keyset-and-dict-01.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/05-vec-keyset-and-dict.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating a vec. function get_items(): vec<string> { $items = vec['a', 'b', 'c']; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/05-vec-keyset-and-dict-02.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/05-vec-keyset-and-dict.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $items = vec['a', 'b', 'c']; // Accessing items by index. $items[0]; // 'a' $items[3]; // throws OutOfBoundsException // Accessing items that might be out-of-bounds. idx($items, 0); // 'a' idx($items, 3); // null idx($items, 3, 'default'); // 'default' // Modifying items. These operations set $items // to a modified copy, and do not modify the original value. $items[0] = 'xx'; // vec['xx', 'b', 'c'] $items[] = 'd'; // vec['xx', 'b', 'c', 'd'] // Getting the length. C\count($items); // 4 // Seeing if a vec contains a value or index. C\contains($items, 'a'); // true C\contains_key($items, 2); // true // Iterating. foreach ($items as $item) { echo $item; } // Iterating with the index. foreach ($items as $index => $item) { echo $index; // e.g. 0 echo $item; // e.g. 'a' } // Equality checks. Elements are recursively compared with ===. vec[1] === vec[1]; // true vec[1, 2] === vec[2, 1]; // false // Combining vecs. Vec\concat(vec[1], vec[2, 3]); // vec[1, 2, 3] // Removing items at an index. $items = vec['a', 'b', 'c']; $n = 1; Vec\concat(Vec\take($items, $n), Vec\drop($items, $n + 1)); // vec['a', 'c'] // Converting from an Iterable. vec(keyset[10, 11]); // vec[10, 11] vec(Vector { 20, 21 }); // vec[20, 21] vec(dict['key1' => 'value1']); // vec['value1'] // Type checks. $items is vec<_>; // true }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/05-vec-keyset-and-dict-03.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/05-vec-keyset-and-dict.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating a keyset. function get_items(): keyset<string> { $items = keyset['a', 'b', 'c']; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/05-vec-keyset-and-dict-04.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/05-vec-keyset-and-dict.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $items = keyset['a', 'b', 'c']; // Checking if a keyset contains a value. C\contains($items, 'a'); // true // Adding/removing items. These operations set $items to a modified copy, // and do not modify the original value. $items[] = 'd'; // keyset['a', 'b', 'c', 'd'] $items[] = 'a'; // keyset['a', 'b', 'c', 'd'] unset($items['b']); // keyset['a', 'c', 'd'] // Getting the length. C\count($items); // 3 // Iterating. foreach ($items as $item) { echo $item; } // Equality checks. === returns false if the order does not match. keyset[1] === keyset[1]; // true keyset[1, 2] === keyset[2, 1]; // false Keyset\equal(keyset[1, 2], keyset[2, 1]); // true // Combining keysets. Keyset\union(keyset[1, 2], keyset[2, 3]); // keyset[1, 2, 3] // Converting from an Iterable. keyset(vec[1, 2, 1]); // keyset[1, 2] keyset(Vector { 20, 21 }); // keyset[20, 21] keyset(dict['key1' => 'value1']); // keyset['value1'] // Type checks. $items is keyset<_>; // true }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/05-vec-keyset-and-dict-05.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/05-vec-keyset-and-dict.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating a dict. function get_items(): dict<string, int> { $items = dict['a' => 1, 'b' => 3]; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/05-vec-keyset-and-dict-06.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/05-vec-keyset-and-dict.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $items = dict['a' => 1, 'b' => 3]; // Accessing items by key. $items['a']; // 1 $items['foo']; // throws OutOfBoundsException // Accessing keys that may be absent. idx($items, 'a'); // 1 idx($items, 'z'); // null idx($items, 'z', 'default'); // 'default' // Inserting, updating or removing values in a dict. These operations // set $items to a modified copy, and do not modify the original value. $items['a'] = 42; // dict['a' => 42, 'b' => 3] $items['z'] = 100; // dict['a' => 42, 'b' => 3, 'z' => 100] unset($items['b']); // dict['a' => 42, 'z' => 100] // Getting the keys. Vec\keys(dict['a' => 1, 'b' => 3]); // vec['a', 'b'] // Getting the values. vec(dict['a' => 1, 'b' => 3]); // vec[1, 3] // Getting the length. C\count($items); // 2 // Checking if a dict contains a key or value. C\contains_key($items, 'a'); // true C\contains($items, 3); // true // Iterating values. foreach ($items as $value) { echo $value; // e.g. 1 } // Iterating keys and values. foreach ($items as $key => $value) { echo $key; // e.g. 'a' echo $value; // e.g. 1 } // Equality checks. === returns false if the order does not match. dict[] === dict[]; // true dict[0 => 10, 1 => 11] === dict[1 => 11, 0 => 10]; // false Dict\equal(dict[0 => 10, 1 => 11], dict[1 => 11, 0 => 10]); // true // Combining dicts (last item wins). Dict\merge(dict[10 => 1, 20 => 2], dict[30 => 3, 10 => 0]); // dict[10 => 0, 20 => 2, 30 => 3]; // Converting from an Iterable. dict(vec['a', 'b']); // dict[0 => 'a', 1 => 'b'] dict(Map {'a' => 5}); // dict['a' => 5] // Type checks. $items is dict<_, _>; // true }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-01.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating a Vector. function get_items(): Vector<string> { $items = Vector {'a', 'b', 'c'}; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-02.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $items = Vector {'a', 'b', 'c'}; // Accessing items by index. $items[0]; // 'a' $items[3]; // throws OutOfBoundsException // Accessing items that might be out-of-bounds. idx($items, 0); // 'a' idx($items, 3); // null idx($items, 3, 'default'); // 'default' // Modifying items. This mutates the Vector in place. $items[0] = 'xx'; // Vector {'xx', 'b', 'c'} $items[] = 'd'; // Vector {'xx', 'b', 'c', 'd'} // Getting the length. C\count($items); // 4 // Iterating. foreach ($items as $item) { echo $item; } // Iterating with the index. foreach ($items as $index => $item) { echo $index; // e.g. 0 echo $item; // e.g. 'a' } // Equality checks compare references. $items === $items; // true Vector {} === Vector {}; // false // Converting from an Iterable. new Vector(vec[1, 2]); // Vector {1, 2} new Vector(Set {1, 2}); // Vector {1, 2} new Vector(dict['key1' => 'value1']); // Vector {'value1'} // Type checks $items is Vector<_>; // true }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-03.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating an ImmVector. function get_items(): ImmVector<string> { $items = ImmVector {'a', 'b', 'c'}; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-04.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating a Set. function get_items(): Set<string> { $items = Set {'a', 'b', 'c'}; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-05.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $items = Set {'a', 'b', 'c'}; // Checking if a Set contains a value. C\contains_key($items, 'a'); // true // Modifying items. This mutates the Set in place. $items[] = 'd'; // Set {'a', 'b', 'c', 'd'} $items[] = 'a'; // Set {'a', 'b', 'c', 'd'} // Getting the length. C\count($items); // 4 // Iterating. foreach ($items as $item) { echo $item; } // Equality checks compare references. $items === $items; // true Set {} === Set {}; // false // Converting from an Iterable. new Set(vec[1, 2, 1]); // Set {1, 2} new Set(Vector {20, 21}); // Set {20, 21} new Set(dict['key1' => 'value1']); // Set {'value1'} // Type checks. $items is Set<_>; // true }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-06.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating an ImmSet. function get_items(): ImmSet<string> { $items = ImmSet {'a', 'b', 'c'}; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-07.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating a Map. function get_items(): Map<string, int> { $items = Map {'a' => 1, 'b' => 3}; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-08.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $items = Map {'a' => 1, 'b' => 3}; // Accessing items by key. $items['a']; // 1 $items['z']; // throws OutOfBoundsException // Accessing keys that may be absent. idx($items, 'a'); // 1 idx($items, 'z'); // null idx($items, 'z', 'default'); // 'default' // Modifying items. This mutates the Map in place. $items['a'] = 42; // Map {'a' => 42, 'b' => 3} $items['z'] = 100; // Map {'a' => 42, 'b' => 3, 'z' => 100} // Getting the keys. Vec\keys(Map {'a' => 1, 'b' => 3}); // vec['a', 'b'] // Getting the values. vec(Map {'a' => 1, 'b' => 3}); // vec[1, 3] // Getting the length. C\count($items); // 3 // Iterating values. foreach ($items as $value) { echo $value; } // Iterating keys and values. foreach ($items as $key => $value) { echo $key; echo $value; } // Equality checks compare references. $items === $items; // true Map {} === Map {}; // false // Converting from an Iterable. new Map(dict['key1' => 'value1']); // Map { 'key1' => 'value1'} new Map(vec['a', 'b']); // Map {0 => 'a', 1 => 'b'} // Type checks $items is Map<_, _>; // true }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-09.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Creating an ImmMap. function get_items(): ImmMap<string, int> { $items = ImmMap {'a' => 1, 'b' => 3}; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-10.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function get_items(): Pair<int, string> { $items = Pair {42, 'foo'}; return $items; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/10-object-collections-11.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/10-object-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function example_snippet_wrapper(): Awaitable<void> { $items = Pair {42, 'foo'}; // Destructuring a Pair value. list($x, $y) = $items; // $x: 42, $y: 'foo' // Accessing elements by index. $items[0]; // 42 $items[1]; // 'foo' }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/15-varray-and-darray-01.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/15-varray-and-darray.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function get_items(bool $b): varray_or_darray<int, string> { if ($b) { return varray['a', 'b']; } else { return darray[5 => 'c']; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/20-mutating-values-01.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/20-mutating-values.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function update_value(vec<int> $items): vec<int> { // Both of these operations set $items a modified copy of the // original value. $items[0] = 42; $items[] = 100; return $items; } function demo(): void { $v = vec[1, 2]; // $v is unaffected by this function call. $v2 = update_value($v); var_dump($v); // vec[1, 2] var_dump($v2); // vec[42, 2, 100] }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/20-mutating-values-02.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/20-mutating-values.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Person { public function __construct(public string $name) {} } function update_person(vec<Person> $items): void { $items[0]->name = "new"; } function demo(): void { $v = vec[new Person("old")]; update_person($v); var_dump($v); // vec[Person { name: "new" }] }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/20-mutating-values-03.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/20-mutating-values.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function update_value(inout vec<int> $items): void { $items[0] = 42; $items[] = 100; } function demo(): void { $v = vec[1, 2]; update_value(inout $v); var_dump($v); // vec[42, 2, 100] }
Hack
hhvm/hphp/hack/test/extracted_from_manual/08-arrays-and-collections/20-mutating-values-04.hack
// @generated by hh_manual from manual/hack/08-arrays-and-collections/20-mutating-values.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function update_value(Vector<int> $items): void { $items[0] = 42; $items[] = 100; } function demo(): void { $v = Vector {1, 2}; update_value($v); var_dump($v); // Vector {42, 2, 100} }