language
stringlengths
0
24
filename
stringlengths
9
214
code
stringlengths
99
9.93M
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/45-soft-types-01.hack
// @generated by hh_manual from manual/hack/10-types/45-soft-types.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function probably_int(<<__Soft>> int $x): @int { return $x + 1; } function definitely_int(int $x): int { return $x + 1; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/46-generic-types-stack.hack
// @generated by hh_manual from manual/hack/10-types/46-generic-types.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class StackUnderflowException extends Exception {} class Stack<T> { private vec<T> $stack; private int $stackPtr; public function __construct() { $this->stackPtr = 0; $this->stack = vec[]; } public function push(T $value): void { $this->stack[] = $value; $this->stackPtr++; } public function pop(): T { if ($this->stackPtr > 0) { $this->stackPtr--; return $this->stack[$this->stackPtr]; } else { throw new StackUnderflowException(); } } } function use_int_stack(Stack<int> $stInt): void { $stInt->push(10); $stInt->push(20); $stInt->push(30); echo 'pop => '.$stInt->pop()."\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/67-nullable-types-01.hack
// @generated by hh_manual from manual/hack/10-types/67-nullable-types.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_nullable_str(?string $s): string { if ($s is null){ return "default"; } else { return $s; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/67-nullable-types-02.hack
// @generated by hh_manual from manual/hack/10-types/67-nullable-types.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_nullable_str2(?string $s): string { if ($s is nonnull){ return $s; } else { return "default"; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/67-nullable-types-03.hack
// @generated by hh_manual from manual/hack/10-types/67-nullable-types.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function my_filter_nulls<T as nonnull>(vec<?T> $items): vec<T> { $result = vec[]; foreach ($items as $item) { if ($item is null) { // Discard it. } else { $result[] = $item; } } return $result; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/79-type-aliases-01.hack
// @generated by hh_manual from manual/hack/10-types/79-type-aliases.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ type Complex = shape('real' => float, 'imag' => float); newtype Point = (float, float);
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/79-type-aliases-02.hack
// @generated by hh_manual from manual/hack/10-types/79-type-aliases.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ newtype Point = (float, float); function create_Point(float $x, float $y): Point { return tuple($x, $y); } function distance(Point $p1, Point $p2): float { $dx = $p1[0] - $p2[0]; $dy = $p1[1] - $p2[1]; return sqrt($dx*$dx + $dy*$dy); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/79-type-aliases-03.hack
// @generated by hh_manual from manual/hack/10-types/79-type-aliases.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ newtype Widget = int;
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/79-type-aliases-04.hack
// @generated by hh_manual from manual/hack/10-types/79-type-aliases.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ newtype Counter = int;
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/79-type-aliases-05.hack
// @generated by hh_manual from manual/hack/10-types/79-type-aliases.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ newtype Counter as int = int;
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/85-type-refinement-01.hack
// @generated by hh_manual from manual/hack/10-types/85-type-refinement.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f1(?int $p): void { // $x = $p % 3; // rejected; % not defined for ?int if ($p is int) { // type refinement occurs; $p has type int $x = $p % 3; // accepted; % defined for int } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/85-type-refinement-02.hack
// @generated by hh_manual from manual/hack/10-types/85-type-refinement.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f2(?int $p): void { if ($p is null) { // type refinement occurs; $p has type null // $x = $p % 3; // rejected; % not defined for null } else { // type refinement occurs; $p has type int $x = $p % 3; // accepted; % defined for int } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/85-type-refinement-03.hack
// @generated by hh_manual from manual/hack/10-types/85-type-refinement.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f3(?int $p): void { if (!$p is null) { // type refinement occurs; $p has type int $x = $p % 3; // accepted; % defined for int } if ($p is nonnull) { // type refinement occurs; $p has type int $x = $p % 3; // accepted; % defined for int } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/85-type-refinement-04.hack
// @generated by hh_manual from manual/hack/10-types/85-type-refinement.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f4(?num $p): void { if (($p is int) || ($p is float)) { // $x = $p**2; // rejected } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/85-type-refinement-05.hack
// @generated by hh_manual from manual/hack/10-types/85-type-refinement.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C { private ?int $p = 8; // holds an int, but type is ?int public function m(): void { if ($this->p is int) { // type refinement occurs; $this->p is int $x = $this->p << 2; // allowed; type is int $this->n(); // could involve a permanent type refinement on $p // $x = $this->p << 2; // disallowed; might no longer be int } } public function n(): void { /* ... */ } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/86-with-refinement-box-with-type+ctx.hack
// @generated by hh_manual from manual/hack/10-types/86-with-refinement.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ interface Box { abstract const type T; abstract const ctx C super [defaults]; public function get()[this::C]: this::T; } function unwrap_as_singleton_set<Tb as arraykey>( Box with { type T = Tb } $int_or_string_box ): Set<Tb> { return Set { $int_or_string_box->get() }; } function unwrap_pure<Tb>( Box with { ctx C = []; type T = Tb } $box, )[]: Tb { return $box->get(); // OK (type-checker knows `get` must have the empty context list) } // API function wrap_number(num $x): Box with { type T = num } { return new IntBox($x); } // implementation details (subject to change): class IntBox implements Box { const type T = num; const ctx C = []; public function __construct(private this::T $v) {} public function get()[]: this::T { return $this->v; } } function boxed_sum( Traversable<Box with { type T as num }> $numeric_boxes ): float { $sum = 0.0; foreach ($numeric_boxes as $nb) { $sum += $nb->get(); } return $sum; } interface MeasuredBox extends Box { abstract const type TQuantity; public function getQuantity(): this::TQuantity; } function weigh_bulk_unsafe<TBox as MeasuredBox>(TBox $box): float where TBox::TQuantity = float { return $box->getQuantity(); } function weigh_bulk(MeasuredBox with { type TQuantity = float } $box): float { return $box->getQuantity(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/88-type-inferencing-01.hack
// @generated by hh_manual from manual/hack/10-types/88-type-inferencing.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function foo(int $i): void { $v = 100; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/88-type-inferencing-02.hack
// @generated by hh_manual from manual/hack/10-types/88-type-inferencing.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function g(int $p1 = -1): void { // on entry to the function, $p1 has the declared type int // ... $p1 = 23.56; // $p1 has type float // ... }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/88-type-inferencing-03.hack
// @generated by hh_manual from manual/hack/10-types/88-type-inferencing.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> { $doubler = $p ==> $p * 2; $doubler(3); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/88-type-inferencing-04.hack
// @generated by hh_manual from manual/hack/10-types/88-type-inferencing.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> { $doubler = (int $p) ==> $p * 2; $doubler = ($p = 0) ==> $p * 2; $doubler = ($p): int ==> $p * 2; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/10-types/88-type-inferencing-c.hack
// @generated by hh_manual from manual/hack/10-types/88-type-inferencing.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f(): void { $v = 'acb'; // $v has type string // ... $v = true; // $v has type bool // ... $v = dict['red' => 10, 'green' => 15]; // $v has type dict<string, int> // ... $v = new C(); // $v has type C } class C { const C1 = 10; // type int inferred from initializer const string C2 = "red"; // type string declared }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/04-bool-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/04-bool.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function is_leap_year(int $yy): bool { return ((($yy & 3) === 0) && (($yy % 100) !== 0)) || (($yy % 400) === 0); } <<__EntryPoint>> function main(): void { $result = is_leap_year(1900); echo "1900 is ".(($result === true) ? "" : "not ")."a leap year\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/07-int-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/07-int.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function is_leap_year(int $yy): bool { return ((($yy & 3) === 0) && (($yy % 100) !== 0)) || (($yy % 400) === 0); } <<__EntryPoint>> function main(): void { $year = 2001; $result = is_leap_year($year); echo "$year is ".(($result === true) ? "" : "not ")."a leap year\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/10-float-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/10-float.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function average_float(float $p1, float $p2): float { return ($p1 + $p2) / 2.0; } <<__EntryPoint>> function main(): void { $val = 3e6; $result = average_float($val, 5.2E-2); echo "\$result is ".$result."\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/13-num-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/13-num.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Point { private float $x; private float $y; public function __construct(num $x = 0, num $y = 0) { $this->x = (float)$x; $this->y = (float)$y; } public function move(num $x = 0, num $y = 0): void { $this->x = (float)$x; $this->y = (float)$y; } // ... }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/16-string-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/16-string.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ use namespace HH\Lib\{Locale,Str}; <<__EntryPoint>> function main(): void { $haystack = "abc\u{00e9}"; // ends with UTF-8 "LATIN SMALL LETTER E ACUTE" $needle = "\u{0065}\u{0301}"; // "LATIN SMALL LETTER E", "COMBINING ACUTE ACCENT" $locale = Locale\create("en_US.UTF-8"); \var_dump(dict[ 'Byte test' => Str\ends_with($haystack, $needle), // false 'Character test' => Str\ends_with_l($locale, $haystack, $needle), // true 'Strip byte suffix' => Str\strip_suffix($haystack, $needle), // no change 'Strip character suffix' => Str\strip_suffix_l($locale, $haystack, $needle), // removed ]); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/19-void-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/19-void.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function draw_line(Point $p1, Point $p2): void { /* ... */ } class Point { private float $x; private float $y; public function __construct(num $x = 0, num $y = 0) { $this->x = (float)$x; $this->y = (float)$y; } public function move(num $x = 0, num $y = 0): void { $this->x = (float)$x; $this->y = (float)$y; } // ... } <<__EntryPoint>> function main(): void { draw_line(new Point(1.2, 3.3), new Point(6.2, -4.5)); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/19-void-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/19-void.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Stack<T> { // ... public function push(T $value): void { /* ... */ } // ... }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/25-tuples-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/25-tuples.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> { $t = tuple(10, true, 2.3); echo "\$t[2] = >" . $t[2] . "<"; // outputs "$t[2] = >2.3<" $t[0] = 99; // change 10 to 99 }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/25-tuples-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/25-tuples.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ newtype Point = (float, float); function create_point(float $x, float $y): Point { return tuple($x, $y); } function distance(Point $p1, Point $p2): float { $dx = $p1[0] - $p2[0]; $dy = $p1[1] - $p2[1]; return sqrt($dx * $dx + $dy * $dy); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.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> { $my_point = shape('x' => -3, 'y' => 6, 'visible' => true); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.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> { $server = shape('name' => 'db-01', 'age' => 365); $empty = shape(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-03.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.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> { $s1 = shape('name' => 'db-01', 'age' => 365); $s2 = $s1; $s2['age'] = 42; // $s1['age'] is still 365. }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-04.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.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> { // $s has type shape(). $s = shape(); // $s now has type shape('name' => string). $s['name'] = 'db-01'; // $s now has type shape('name' => string, 'age' => int). $s['age'] = 365; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-05.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.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> { $s1 = shape('name' => 'db-01', 'age' => 365); $s2 = shape('age' => 365, 'name' => 'db-01'); $s1 === $s2; // false }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-06.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_server(shape('name' => string, 'age' => int) $s): void { // ... }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-07.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ type Server = shape('name' => string, 'age' => int); // Equivalent to the previous takes_server function. function takes_server(Server $s): void { // ... return; }
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-08.hack_error
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_server(Server $s): void { return; } function test(): void { $args = shape('name' => 'hello', 'age' => 10); takes_server($args); // no error $args = shape('name' => null, 'age' => 10); takes_server($args); // type error: field type mismatch $args = shape('name' => 'hello', 'age' => 10, 'error' => true); takes_server($args); // type error: extra field }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-09.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.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> { // $s has type shape('name' => string, 'age' => int). $s = shape('name' => 'db-01', 'age' => 365); // $s now has type shape('name' => string, 'age' => string). $s['age'] = '1 year'; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-10.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ type Server = shape('name' => string, 'age' => int); type Pet = shape('name' => string, 'age' => int); function takes_server(Server $_): void {} function takes_pet(Pet $p): void { // No error here. takes_server($p); }
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-11.hack_error
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_named(shape('name' => string) $_): void {} function demo(): void { takes_named(shape('name' => 'db-01', 'age' => 365)); // type error }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-12.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_named(shape('name' => string, ...) $_): void {} // OK. function demo(): void { takes_named(shape('name' => 'db-01', 'age' => 365)); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-13.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_named(shape('name' => string, ...) $n): void { // The value in the shape, or null if field is absent. $nullable_age = Shapes::idx($n, 'age'); // The value in the shape, or 0 if field is absent. $age_with_default = Shapes::idx($n, 'age', 0); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-14.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_server(shape('name' => string, ?'age' => int) $s): void { $age = Shapes::idx($s, 'age', 0); } function example_usage(): void { takes_server(shape('name' => 'db-01', 'age' => 365)); takes_server(shape('name' => 'db-02')); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-15.hack
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_server2(shape('name' => string, 'age' => ?int) $s): void { $age = $s['age'] ?? 0; } function takes_server3(shape('name' => string, ...) $s): void { $age = Shapes::idx($s, 'age', 0) as int; }
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/28-shape-16.hack_error
// @generated by hh_manual from manual/hack/11-built-in-types/28-shape.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // This produces a typehint violation at runtime. function returns_int_instead(): shape('x' => int) { return 1; } // No runtime error. function returns_wrong_shape(): shape('x' => int) { return shape('y' => 1); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/31-arraykey-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/31-arraykey.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function process_key(arraykey $p): void { if ($p is int) { // we have an int } else { // we have a string } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum Colors: int { Red = 3; Green = 5; Blue = 10; Default = 3; // duplicate value is okay }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum Permission: string { Read = 'R'; Write = 'W'; Execute = 'E'; Delete = 'D'; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-03.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum BitFlags: int as int { F1 = 1; // value 1 F2 = BitFlags::F1 << 1; // value 2 F3 = BitFlags::F2 << 1; // value 4 F4 = 4 + 3; // value 7 }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-04.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum Position: int { Top = 0; Bottom = 1; Left = 2; Right = 3; Center = 4; } function writeText(string $text, Position $pos): void { switch ($pos) { case Position::Top: // ... break; case Position::Center: // ... break; case Position::Right: // ... break; case Position::Left: // ... break; case Position::Bottom: // ... break; } } <<__EntryPoint>> function main(): void { writeText("Hello", Position::Bottom); writeText("Today", Position::Left); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-05.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum Position: int { Top = 0; Bottom = 1; Left = 2; Right = 3; Center = 4; } <<__EntryPoint>> function main(): void { $names = Position::getNames(); echo " Position::getNames() ---\n"; foreach ($names as $key => $value) { echo "\tkey >$key< has value >$value<\n"; } $values = Position::getValues(); echo "Position::getValues() ---\n"; foreach ($values as $key => $value) { echo "\tkey >$key< has value >$value<\n"; } Dict\flip(Position::getValues()); // safe flip of values as keys }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-06.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum Bits: int { B1 = 2; B2 = 4; B3 = 8; } <<__EntryPoint>> function main(): void { Bits::assert(2); // 2 Bits::assert(16); // UnexpectedValueException Bits::coerce(2); // 2 Bits::coerce(2.0); // null Bits::coerce(16); // null }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-07.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum Bits: int { B1 = 2; B2 = 4; B3 = 8; } <<__EntryPoint>> function main(): void { $all_values = vec[2, 4, 8]; $some_values = vec[2, 4, 16]; $no_values = vec[32, 64, 128]; Bits::assertAll($all_values); // vec[2, 4, 8] Bits::assertAll($some_values); // throws on 16, UnexpectedValueException Bits::assertAll($no_values); // throws on 32, UnexpectedValueException }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-08.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum Bits: int { B1 = 2; B2 = 4; B3 = 8; } <<__EntryPoint>> function main(): void { \var_dump(Bits::isValid(2)); \var_dump(Bits::isValid(2.0)); \var_dump(Bits::isValid("2.0")); \var_dump(Bits::isValid(8)); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-09.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum MyEnum: int { FOO = 1; } <<__EntryPoint>> function main(): void { 1 is MyEnum; // true 1 as MyEnum; // 1 42 is MyEnum; // false 42 as MyEnum; // TypeAssertionException 'foo' is MyEnum; // false 'foo' as MyEnum; // TypeAssertionException '1' is MyEnum; // CAUTION - true '1' as MyEnum; // CAUTION - '1' }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/34-enum-10.hack
// @generated by hh_manual from manual/hack/11-built-in-types/34-enum.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum E1: int as int { A = 0; } enum E2: int as int { B = 1; } enum F: int { // same-line alternative: use E1, E2; use E1; use E2; C = 2; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Enum class where we allow any type enum class Random: mixed { int X = 42; string S = 'foo'; } // Enum class that mimics a normal enum (only allowing ints) enum class Ints: int { int A = 0; int B = 10; }
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-02.hack_error
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum class Foo: string { string BAR = 'BAZ'; } function do_stuff(Foo $value): void { var_dump($value); } function main(): void { do_stuff(Foo::BAR); // expected Foo but got string }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-03.hack
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum class Foo: string { string BAR = 'BAZ'; } function do_stuff(HH\MemberOf<Foo, string> $value): void { var_dump($value); } function main(): void { do_stuff(Foo::BAR); // ok }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-04.hack
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum E: int { A = 42; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-05.hack
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum class EC: int { int A = 42; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-06.hack
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ interface IGet<+T> { public function get(): T; } class Box<T> implements IGet<T> { public function __construct(private T $data)[] {} public function get(): T { return $this->data; } public function set(T $data): void { $this->data = $data; } } abstract enum class E : IGet<mixed> { abstract const type T; abstract Box<this::T> A; Box<int> B = new Box(42); } enum class F : IGet<mixed> extends E { const type T = string; Box<this::T> A = new Box('zuck'); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-dep_dict.hack
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function expect_string(string $str): void { echo 'expect_string called with: '.$str."\n"; } interface IKey { public function name(): string; } abstract class Key<T> implements IKey { public function __construct(private string $name)[] {} public function name(): string { return $this->name; } public abstract function coerceTo(mixed $data): T; } class IntKey extends Key<int> { public function coerceTo(mixed $data): int { return $data as int; } } class StringKey extends Key<string> { public function coerceTo(mixed $data): string { // random logic can be implemented here $s = $data as string; // let's make everything in caps return Str\capitalize($s); } } enum class EKeys: IKey { // here are a default key, but this could be left empty Key<string> NAME = new StringKey('NAME'); } abstract class DictBase { // type of the keys, left abstract for now abstract const type TKeys as EKeys; // actual data storage private dict<string, mixed> $raw_data = dict[]; // generic code written once which enforces type safety public function get<T>(\HH\MemberOf<this::TKeys, Key<T>> $key): ?T { $name = $key->name(); $raw_data = idx($this->raw_data, $name); // key might not be set if ($raw_data is nonnull) { $data = $key->coerceTo($raw_data); return $data; } return null; } public function set<T>(\HH\MemberOf<this::TKeys, Key<T>> $key, T $data): void { $name = $key->name(); $this->raw_data[$name] = $data; } } class Foo { /* user code in here */ } class MyKeyType extends Key<Foo> { public function coerceTo(mixed $data): Foo { // user code validation return $data as Foo; } } enum class MyKeys: IKey extends EKeys { Key<int> AGE = new IntKey('AGE'); MyKeyType BLI = new MyKeyType('BLI'); } class MyDict extends DictBase { const type TKeys = MyKeys; } <<__EntryPoint>> function main(): void { $d = new MyDict(); $d->set(MyKeys::NAME, 'tony'); $d->set(MyKeys::BLI, new Foo()); // $d->set(MyKeys::AGE, new Foo()); // type error expect_string($d->get(MyKeys::NAME) as nonnull); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-extend.hack
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ interface IBox {} class Box<T> implements IBox { public function __construct(public T $data)[] {} } enum class Boxes: IBox { Box<int> Age = new Box(42); Box<string> Color = new Box('red'); Box<int> Year = new Box(2021); } function get<T>(\HH\MemberOf<Boxes, Box<T>> $box): T { return $box->data; } function test0(): void { get(Boxes::Age); // ok, of type int, returns 42 get(Boxes::Color); // ok, of type string, returns 'red' get(Boxes::Year); // ok, of type int, returns 2021 } enum class EBase: IBox { Box<int> Age = new Box(42); } enum class EExtend: IBox extends EBase { Box<string> Color = new Box('red'); } enum class E: IBox { Box<int> Age = new Box(42); } enum class F: IBox { Box<string> Name = new Box('foo'); } enum class X: IBox extends E, F { } // ok, no ambiguity enum class E0: IBox extends E { Box<int> Color = new Box(0); } enum class E1: IBox extends E { Box<string> Color = new Box('red'); } // enum class Y: IBox extends E0, E1 { } // type error, Y::Color is declared twice, in E0 and in E1 // only he name is use for ambiguity enum class DiamondBase: IBox { Box<int> Age = new Box(42); } enum class D1: IBox extends DiamondBase { Box<string> Name1 = new Box('foo'); } enum class D2: IBox extends DiamondBase { Box<string> Name2 = new Box('bar'); } enum class D3: IBox extends D1, D2 {} <<__EntryPoint>> function main(): void { echo D3::Age->data; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/35-enum-class-hasname.hack
// @generated by hh_manual from manual/hack/11-built-in-types/35-enum-class.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Some class definitions to make a more involved example interface IHasName { public function name(): string; } class HasName implements IHasName { public function __construct(private string $name)[] {} public function name(): string { return $this->name; } } class ConstName implements IHasName { public function name(): string { return "bar"; } } // enum class which base type is the IHasName interface: each enum value // can be any subtype of IHasName, here we see HasName and ConstName enum class Names: IHasName { HasName Hello = new HasName('hello'); HasName World = new HasName('world'); ConstName Bar = new ConstName(); } // abstract enum class with some abstract members abstract enum class AbstractNames: IHasName { abstract HasName Foo; HasName Bar = new HasName('bar'); } enum class ConcreteNames: IHasName extends AbstractNames { HasName Foo = new HasName('foo'); // one must provide all the abstract members // Bar is inherited from AbstractNames }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/36-enum-class-label-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/36-enum-class-label.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ enum E: int { A = 42; B = 42; } function f(E $value): void { switch($value) { case E::A: echo "A "; break; case E::B: echo "B "; break; } echo $value . "\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/36-enum-class-label-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/36-enum-class-label.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ newtype Label<-TEnumClass, TType> = mixed;
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/36-enum-class-label-03.hack
// @generated by hh_manual from manual/hack/11-built-in-types/36-enum-class-label.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo {} enum class F: Foo { Foo A = new Foo(); } // E#A === E#B is false // E#A === F#A is true
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/36-enum-class-label-04.hack
// @generated by hh_manual from manual/hack/11-built-in-types/36-enum-class-label.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ newtype MemberOf<-TEnumClass, +TType> as TType = TType; newtype Label<-TEnumClass, TType> = mixed; class A {} class B extends A {} enum class G: A { A X = new A(); B Y = new B(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/36-enum-class-label-label.hack
// @generated by hh_manual from manual/hack/11-built-in-types/36-enum-class-label.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // We are using int here for readability but it works for any type enum class E: int { int A = 42; int B = 42; } function full_print(\HH\EnumClass\Label<E, int> $label): void { echo E::nameOf($label) . " "; echo E::valueOf($label) . "\n"; } function partial_print(\HH\MemberOf<E, int> $value): void { echo $value . "\n"; } function test_eq(\HH\EnumClass\Label<E, int> $label): void { if ($label === E#A) { echo "label is A\n"; } switch ($label) { case E#A: break; case E#B: break; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/49-this-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/49-this.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ interface I1 { abstract const type T1 as arraykey; public function get_ID(): this::T1; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/55-classname-employee.hack
// @generated by hh_manual from manual/hack/11-built-in-types/55-classname.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<__ConsistentConstruct>> class Employee { // ... } function f(classname<Employee> $clsname): void { $w = new $clsname(); // create an object whose type is passed in }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/56-darray-varray-runtime-options-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/56-darray-varray-runtime-options.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function get_all_runtime_options( ): dict<string, shape( 'global_value' => string, 'local_value' => string, 'access' => string, )> { return \ini_get_all() |> Dict\filter_keys($$, $name ==> Str\contains($name, 'hack_arr')); } <<__EntryPoint>> async function main_async(): Awaitable<void> { foreach (get_all_runtime_options() as $name => $values) { echo Str\format( "%s> global_value(%s), local_value(%s), access(%s)\n", Str\pad_right($name, 60, '-'), $values['global_value'], $values['local_value'], $values['access'], ); } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/56-darray-varray-runtime-options-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/56-darray-varray-runtime-options.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> { $_ = dict[] is shape(); $_ = vec[42] is /*tuple*/(int); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/61-null-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/61-null.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function number_or_default(?int $x): int { if ($x is null) { return 42; } else { return $x; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/61-null-traversefrom.hack
// @generated by hh_manual from manual/hack/11-built-in-types/61-null.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // A toy interface that allows you to iterate over something, // setting a start point. interface TraverseFrom<Tv, Ti> { public function startAt(Ti $_): Traversable<Tv>; } class TraverseIntsFromStart implements TraverseFrom<int, null> { public function __construct(private vec<int> $items) {} public function startAt(null $_): Traversable<int> { return $this->items; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/71-dynamic-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/71-dynamic.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f(dynamic $x) : void { $n = $x + 5; // $n is a num $s = $x . "hello!"; // $s is a string $y = $x->anyMethod(); // $y is dynamic }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/71-dynamic-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/71-dynamic.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f(dynamic $d): void {} function g(arraykey $a): void {} function caller(int $i): void { f($i); // int ~> dynamic g($i); // int ~> arraykey by subtyping }
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/71-dynamic-03.hack_error
// @generated by hh_manual from manual/hack/11-built-in-types/71-dynamic.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function enforced(int $i): void {} function notEnforced(shape('a' => int) $s): void {} function caller(dynamic $d): void { enforced($d); // dynamic ~> int, runtime will throw if $d is not an int notEnforced($d); // Hack error, dynamic ~/> shape('a' => int), runtime will not throw if $d is not a shape } ```.hhconfig coercion_from_dynamic = true
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/71-dynamic-04.hack_error
// @generated by hh_manual from manual/hack/11-built-in-types/71-dynamic.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function expect_int(int $i): void {} function expect_string(string $s): void {} function choose(bool $b, dynamic $d, int $i): void { if ($b) { $x = $d; } else { $x = $i; } expect_int($x); // (dynamic | int) ~> int // dynamic ~> int because int is enforced // int ~> int because int <: int expect_string($x); // Hack error, (dynamic | int) ~/> string because int ~/> string } ```.hhconfig coercion_from_dynamic = true coercion_from_union = true complex_coercion = true
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/73-noreturn-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/73-noreturn.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function something_went_wrong(): noreturn { throw new Exception('something went wrong'); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/73-noreturn-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/73-noreturn.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<__EntryPoint>> async function main_async(): Awaitable<void> { $nullable_int = '_' ? 0 : null; if (!($nullable_int is nonnull)) { invariant_violation('$nullable_int must not be null'); } // If we didn't fall into the if above, $nullable_int must be an int. takes_int($nullable_int); } function takes_int(int $int): void { echo $int; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/73-noreturn-03.hack
// @generated by hh_manual from manual/hack/11-built-in-types/73-noreturn.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function i_am_a_noreturn_function(): noreturn { throw new Exception('stop right here'); } function i_return_nothing(): nothing { i_am_a_noreturn_function(); } const ?int NULLABLE_INT = 0; async function main_async(): Awaitable<void> { example_noreturn(); example_nothing(); } function example_noreturn(): int { $nullable_int = NULLABLE_INT; if ($nullable_int is null) { i_am_a_noreturn_function(); } return $nullable_int; } function example_nothing(): int { $nullable_int = NULLABLE_INT; if ($nullable_int is null) { return i_return_nothing(); } return $nullable_int; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/74-nothing-01.hack
// @generated by hh_manual from manual/hack/11-built-in-types/74-nothing.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function throw_as_an_expression(\Throwable $t): nothing { throw $t; } function returns_an_int(?int $nullable_int): int { // You can not use a `throw` statement in an expression bodied lambda. // You need to add curly braces to allow a `throw` statement. $throwing_lambda = () ==> { throw new \Exception(); }; $throwing_expr_lambda = () ==> throw_as_an_expression(new \Exception()); // You can't write a statement on the RHS of an operator, because it operates on expressions. // The type of the `??` operator is `(nothing | int)`, which simplifies to `int`, // so this return statement is valid. return $nullable_int ?? throw_as_an_expression(new \Exception()); } <<__EntryPoint>> async function main_async(): Awaitable<void> { echo returns_an_int(1); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/74-nothing-02.hack
// @generated by hh_manual from manual/hack/11-built-in-types/74-nothing.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_vec_of_strings(vec<string> $_): void {} function takes_vec_of_bools(vec<bool> $_): void {} <<__EntryPoint>> async function main_async(): Awaitable<void> { $empty_vec = vec[]; takes_vec_of_bools($empty_vec); takes_vec_of_strings($empty_vec); foreach ($empty_vec as $nothing) { $nothing->whatever(); takes_vec_of_strings($nothing); } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/74-nothing-03.hack
// @generated by hh_manual from manual/hack/11-built-in-types/74-nothing.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ interface DontForgetToImplementShipIt { public function shipIt(nothing $_): mixed; } abstract class Software implements DontForgetToImplementShipIt { } class HHVM extends Software { public function shipIt(string $version): string { return 'Shipping HHVM version '.$version.'!'; } } class HSL extends Software { private function __construct(public bool $has_new_functions) { } public function shipIt(bool $has_new_functions): HSL { return new HSL($has_new_functions); } } class HHAST extends Software { public function shipIt(Container<string> $linters): void { foreach ($linters as $linter) { invariant( Str\ends_with($linter, 'Linter'), 'Linter %s does not have a name that ends in "Linter"!', $linter, ); } } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/74-nothing-04.hack
// @generated by hh_manual from manual/hack/11-built-in-types/74-nothing.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ final class MyClass<-T> { public function consume(T $value): void {} public function someOtherMethod(): void {} } // We don't use the `T` from `->consume(T): void` in the function, // so we can use `nothing` for the generic and accept any and all MyClass instances. function some_function(MyClass<nothing> $a, MyClass<nothing> $b): void { if ($a !== $b) { $b->someOtherMethod(); echo "different\n"; } } <<__EntryPoint>> async function main_async(): Awaitable<void> { $my_class_int = new MyClass<int>(); $my_class_string = new MyClass<string>(); some_function($my_class_int, $my_class_string); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/11-built-in-types/74-nothing-undefined.hack
// @generated by hh_manual from manual/hack/11-built-in-types/74-nothing.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ type undefined = nothing; function undefined(): undefined { throw new Exception('NOT IMPLEMENTED: `undefined` cannot be produced.'); } interface MyInterface { public function isAmazed(): bool; } function do_something(MyInterface $my_interface): bool { return $my_interface->isAmazed(); } <<__EntryPoint>> async function main_async(): Awaitable<void> { $my_interface = undefined(); // We won't ever reach this line, since `undefined()` will halt the program by throwing. // We can't produce a MyInterface just yet, since there are no classes which implement it. // `undefined` is a placeholder for now. // We can continue writing our business logic and come back to this later. if (do_something($my_interface)) { // Write the body first, worry about the condition later. } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/01-introduction-01.hack
// @generated by hh_manual from manual/hack/12-generics/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function swap<T>(inout T $i1, inout T $i2): void { $temp = $i1; $i1 = $i2; $i2 = $temp; } <<__EntryPoint>> function main(): void { $v1 = -10; $v2 = 123; echo "\$v1 = ".$v1.", \$v2 = ".$v2."\n"; swap(inout $v1, inout $v2); echo "\$v1 = ".$v1.", \$v2 = ".$v2."\n"; $v3 = "red"; $v4 = "purple"; echo "\$v3 = ".$v3.", \$v4 = ".$v4."\n"; swap(inout $v3, inout $v4); echo "\$v3 = ".$v3.", \$v4 = ".$v4."\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/01-introduction-stack.hack
// @generated by hh_manual from manual/hack/12-generics/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ use namespace HH\Lib\{C, Vec}; interface StackLike<T> { public function isEmpty(): bool; public function push(T $element): void; public function pop(): T; } class StackUnderflowException extends \Exception {} class VecStack<T> implements StackLike<T> { private int $stackPtr; public function __construct(private vec<T> $elements = vec[]) { $this->stackPtr = C\count($elements) - 1; } public function isEmpty(): bool { return $this->stackPtr === -1; } public function push(T $element): void { $this->stackPtr++; if (C\count($this->elements) === $this->stackPtr) { $this->elements[] = $element; } else { $this->elements[$this->stackPtr] = $element; } } public function pop(): T { if ($this->isEmpty()) { throw new StackUnderflowException(); } $element = $this->elements[$this->stackPtr]; $this->elements[$this->stackPtr] = $this->elements[0]; $this->stackPtr--; return $element; } } function useIntStack(StackLike<int> $stInt): void { $stInt->push(10); $stInt->push(20); echo 'pop => '.$stInt->pop()."\n"; // 20 $stInt->push(30); echo 'pop => '.$stInt->pop()."\n"; // 30 echo 'pop => '.$stInt->pop()."\n"; // 10 // $stInt->push(10.5); // rejected as not being type-safe }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/04-type-parameters-01.hack
// @generated by hh_manual from manual/hack/12-generics/04-type-parameters.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function max_value<T>(T $p1, T $p2): T { throw new Exception("unimplemented"); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/07-type-constraints-01.hack
// @generated by hh_manual from manual/hack/12-generics/07-type-constraints.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function max_val<T as num>(T $p1, T $p2): T { return $p1 > $p2 ? $p1 : $p2; } <<__EntryPoint>> function main(): void { echo "max_val(10, 20) = ".max_val(10, 20)."\n"; echo "max_val(15.6, -20.78) = ".max_val(15.6, -20.78)."\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/07-type-constraints-02.hack
// @generated by hh_manual from manual/hack/12-generics/07-type-constraints.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class MyList<T> { public function flatten<Tu>(): MyList<Tu> where T = MyList<Tu> { throw new Exception('unimplemented'); } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/07-type-constraints-03.hack
// @generated by hh_manual from manual/hack/12-generics/07-type-constraints.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class MyList<T> { public function compact<Tu>(): MyList<Tu> where T = ?Tu { throw new Exception('unimplemented'); } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/10-variance-01.hack
// @generated by hh_manual from manual/hack/12-generics/10-variance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // This class is readonly. Had we put in a setter for $this->t, we could not // use covariance. e.g., if we had function setMe(T $x), you would get this // cov.php:9:25,25: Illegal usage of a covariant type parameter (Typing[4120]) // cov.php:7:10,10: This is where the parameter was declared as covariant (+) // cov.php:9:25,25: Function parameters are contravariant class C<+T> { public function __construct(private T $t) {} } class Animal {} class Cat extends Animal {} function f(C<Animal> $p1): void { \var_dump($p1); } function g(varray<Animal> $p1): void { \var_dump($p1); } <<__EntryPoint>> function run(): void { f(new C(new Animal())); f(new C(new Cat())); // accepted g(varray[new Animal(), new Animal()]); g(varray[new Cat(), new Cat(), new Animal()]); // arrays are covariant }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/10-variance-02.hack
// @generated by hh_manual from manual/hack/12-generics/10-variance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // This class is write only. Had we put in a getter for $this->t, we could not // use contravariance. e.g., if we had function getMe(T $x): T, you would get // con.php:10:28,28: Illegal usage of a contravariant type // parameter (Typing[4121]) // con.php:5:10,10: This is where the parameter was declared as // contravariant (-) // con.php:10:28,28: Function return types are covariant class C<-T> { public function __construct(private T $t) {} public function set_me(T $val): void { $this->t = $val; } } class Animal {} class Cat extends Animal {} <<__EntryPoint>> function main(): void { $animal = new Animal(); $cat = new Cat(); $c = new C($cat); // calling set_me with Animal on an instance of C that was initialized with Cat $c->set_me($animal); \var_dump($c); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/12-generics/19-type-erasure-01.hack
// @generated by hh_manual from manual/hack/12-generics/19-type-erasure.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function takes_ints(vec<int> $items): vec<int> { return $items; }
hhvm/hphp/hack/test/extracted_from_manual/12-generics/19-type-erasure-02.hack_error
// @generated by hh_manual from manual/hack/12-generics/19-type-erasure.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function my_foo(): Awaitable<int> { return "not an int"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/01-introduction-01.hack
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function no_listed_contexts()[defaults]: void {/* some fn body */}