language
stringlengths
0
24
filename
stringlengths
9
214
code
stringlengths
99
9.93M
Hack
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/02-local-operations-01.hack
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/02-local-operations.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class SomeClass { public int $i = 0; } function reads_and_writes_prop(SomeClass $sc)[write_props]: void { $sc->i++; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/04-higher-order-functions-01.hack
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/04-higher-order-functions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function has_dependent_fn_arg( (function()[_]: void) $f, )[ctx $f]: void { /* some code */ $f(); /* more code */ }
Hack
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/06-context-constants-01.hack
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/06-context-constants.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ interface IWithConstant { abstract const ctx C; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/08-available-contexts-and-capabilities-01.hack
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/08-available-contexts-and-capabilities.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function does_echo_and_print(): void { echo 'like this'; print 'or like this'; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/08-available-contexts-and-capabilities-02.hack
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/08-available-contexts-and-capabilities.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Valid example class SomeClass { public string $s = ''; public function modifyThis()[write_props]: void { $this->s = 'this applies as well'; } } function can_write_props(SomeClass $sc)[write_props]: void { $sc->s = 'like this'; $sc2 = new SomeClass(); $sc2->s = 'or like this'; }
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/08-available-contexts-and-capabilities-03.hack_error
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/08-available-contexts-and-capabilities.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Invalid example class SomeClass { public string $s = ''; public function modifyThis()[]: void { // pure (empty context list) $this->s = 'this applies as well'; } } function pure_function(SomeClass $sc)[]: void { $sc->s = 'like this'; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/08-available-contexts-and-capabilities-04.hack
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/08-available-contexts-and-capabilities.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function modify_collection()[write_props]: void { $v = Vector {}; $v[] = 'like this'; $m = Map {}; $m['or'] = 'like this'; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/08-available-contexts-and-capabilities-05.hack
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/08-available-contexts-and-capabilities.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Valid example class SomeClass { public static string $s = ''; public function accessStatic()[globals]: void { self::$s; // like this } } function access_static()[globals]: void { SomeClass::$s; // or like this }
hhvm/hphp/hack/test/extracted_from_manual/13-contexts-and-capabilities/08-available-contexts-and-capabilities-06.hack_error
// @generated by hh_manual from manual/hack/13-contexts-and-capabilities/08-available-contexts-and-capabilities.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Invalid example class SomeClass { public static string $s = ''; public function pureMethod()[]: void { self::$s; // like this } } function pure_function()[]: void { SomeClass::$s; // or like this }
Hack
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-01.hack
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function filter<<<__Enforceable>> reify T>(vec<mixed> $list): vec<T> { $ret = vec[]; foreach ($list as $elem) { if ($elem is T) { $ret[] = $elem; } } return $ret; } <<__EntryPoint>> function main(): void { filter<int>(vec[1, "hi", true]); // => vec[1] filter<string>(vec[1, "hi", true]); // => vec["hi"] }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-02.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<__ConsistentConstruct>> abstract class A {} class B extends A {} class C extends A {} function f<<<__Newable>> reify T as A>(): T { return new T(); } <<__EntryPoint>> function main(): void { f<A>(); // not newable since it is abstract class f<B>(); // success f<C>(); // success }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-03.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ final class A<reify T> {} function f<<<__Newable>> reify T as A<string>>(): A<string> { return new T(); } function demo(): void { // creates a new A<int> and since f's return type is A<string>, // this raises a type hint violation f<A<int>>(); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-04.hack
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C { const string class_const = "hi"; public static function h<reify T>(): void {} } // Without reified generics function f<T as C>(classname<T> $x): void { $x::class_const; $x::h<int>(); } // With reified generics function g<reify T as C>(): void { T::class_const; T::h<int>(); }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-05.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Box<reify T> {} function foo(): Box<vec<string>> { return new Box<vec<int>>(); // Type hint violation }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-06.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C<reify +Ta> {} // Cannot make the generic covariant
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-07.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C<reify T> { public static function f(): void { return new T(); // Cannot use T } }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-08.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f<<<__Enforceable>> reify T>(T $x): void { $x is vec<int>; // Cannot use vec<int> $x is T; } function demo(): void { f<vec<int>>(); // not enforceable }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/02-reified-generics-09.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/02-reified-generics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function f<reify T>(): void {} function foo(): void { f(); // need to provide the generics here }
Hack
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/03-reified-generics-migration-01.hack
// @generated by hh_manual from manual/hack/14-reified-generics/03-reified-generics-migration.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C<T> {} function f(C<int> $x): void {} function demo(): void { f(new C()); // OK }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/03-reified-generics-migration-02.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/03-reified-generics-migration.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C<<<__Soft>> reify T> {} function f(C<int> $x): void {} function demo(): void { f(new C<string>()); // Typechecker error: string incompatible with int }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/03-reified-generics-migration-03.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/03-reified-generics-migration.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C<<<__Warn>> reify T> {} function f(C<int> $x): void {} function demo(): void { f(new C<string>()); // Runtime warning: string incompatible with int }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/03-reified-generics-migration-04.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/03-reified-generics-migration.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C<<<__Warn>> reify T> {} function f(C< <<__Soft>> int> $x): void {} function demo(): void { f(new C<string>()); // Runtime warning: string incompatible with int }
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/03-reified-generics-migration-05.hack_error
// @generated by hh_manual from manual/hack/14-reified-generics/03-reified-generics-migration.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C<reify T> {} function f(C< <<__Soft>> int> $x): void {} function demo(): void { f(new C<string>()); // Runtime warning: string incompatible with int }
Hack
hhvm/hphp/hack/test/extracted_from_manual/14-reified-generics/03-reified-generics-migration-06.hack
// @generated by hh_manual from manual/hack/14-reified-generics/03-reified-generics-migration.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C<reify T> {} function f(C<string> $x): void {} function demo(): void { f(new C<string>()); // OK }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/01-introduction-01.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/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\Vec; async function do_cpu_work(): Awaitable<void> { print("Start CPU work\n"); $a = 0; $b = 1; $list = vec[$a, $b]; for ($i = 0; $i < 1000; ++$i) { $c = $a + $b; $list[] = $c; $a = $b; $b = $c; } print("End CPU work\n"); } async function do_sleep(): Awaitable<void> { print("Start sleep\n"); \sleep(1); print("End sleep\n"); } async function run(): Awaitable<void> { print("Start of main()\n"); await Vec\from_async(vec[do_cpu_work(), do_sleep()]); print("End of main()\n"); } <<__EntryPoint>> function main(): void { \HH\Asio\join(run()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/01-introduction-02.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function curl_A(): mixed { $ch = \curl_init(); \curl_setopt($ch, \CURLOPT_URL, "http://example.com/"); \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); return \curl_exec($ch); } function curl_B(): mixed { $ch = \curl_init(); \curl_setopt($ch, \CURLOPT_URL, "http://example.net/"); \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); return \curl_exec($ch); } <<__EntryPoint>> function main(): void { $start = \microtime(true); $a = curl_A(); $b = curl_B(); $end = \microtime(true); echo "Total time taken: ".\strval($end - $start)." seconds\n"; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/01-introduction-03.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/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\Vec; async function curl_A(): Awaitable<string> { $x = await \HH\Asio\curl_exec("http://example.com/"); return $x; } async function curl_B(): Awaitable<string> { $y = await \HH\Asio\curl_exec("http://example.net/"); return $y; } async function async_curl(): Awaitable<void> { $start = \microtime(true); list($a, $b) = await Vec\from_async(vec[curl_A(), curl_B()]); $end = \microtime(true); echo "Total time taken: ".\strval($end - $start)." seconds\n"; } <<__EntryPoint>> function main(): void { \HH\Asio\join(async_curl()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/07-awaitables-01.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/07-awaitables.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 = async { print("Hello, world\n"); return 42; }; \var_dump(await $x); \var_dump(await $x); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/07-awaitables-02.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/07-awaitables.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function foo(): Awaitable<int> { throw new Exception('unimplemented'); } async function demo(): Awaitable<void> { $x = foo(); // $x will be an Awaitable<int> $x = await foo(); // $x will be an int }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/07-awaitables-03.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/07-awaitables.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function f(): Awaitable<int> { return 2; } // We call f() and get back an Awaitable<int> // Once the function is finished executing and we await the awaitable (or in // this case, explicitly join since this call is not in an async function) to get // the explicit result of the function call, we will get back 2. <<__EntryPoint>> function join_main(): void { var_dump(\HH\Asio\join(f())); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/07-awaitables-04.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/07-awaitables.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function foo(): Awaitable<int> { return 3; } <<__EntryPoint>> async function single_awaitable_main(): Awaitable<void> { $aw = foo(); // awaitable of type Awaitable<int> $result = await $aw; // an int after $aw completes var_dump($result); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/07-awaitables-05.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/07-awaitables.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function quads(float $n): Awaitable<float> { return $n * 4.0; } <<__EntryPoint>> async function quads_m(): Awaitable<void> { $awaitables = dict['five' => quads(5.0), 'nine' => quads(9.0)]; $results = await Dict\from_async($awaitables); \var_dump($results['five']); // float(20) \var_dump($results['nine']); // float(36) }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/07-awaitables-06.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/07-awaitables.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function get_raw(string $url): Awaitable<string> { return await \HH\Asio\curl_exec($url); } <<__EntryPoint>> function join_main(): void { $result = \HH\Asio\join(get_raw("http://www.example.com")); \var_dump(\substr($result, 0, 10)); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/10-exceptions-01.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/10-exceptions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function exception_thrower(): Awaitable<void> { throw new \Exception("Return exception handle"); } async function basic_exception(): Awaitable<void> { // the handle does not throw, but result will be an Exception objection. // Remember, this is the same as: // $handle = exception_thrower(); // await $handle; await exception_thrower(); } <<__EntryPoint>> function main(): void { HH\Asio\join(basic_exception()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/10-exceptions-02.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/10-exceptions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function exception_thrower(): Awaitable<void> { throw new \Exception("Return exception handle"); } async function non_exception_thrower(): Awaitable<int> { return 2; } async function multiple_waithandle_exception(): Awaitable<void> { $handles = vec[exception_thrower(), non_exception_thrower()]; // You will get a fatal error here with the exception thrown $results = await Vec\from_async($handles); // This won't happen var_dump($results); } <<__EntryPoint>> function main(): void { HH\Asio\join(multiple_waithandle_exception()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/10-exceptions-03.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/10-exceptions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function exception_thrower(): Awaitable<void> { throw new Exception(); } async function non_exception_thrower(): Awaitable<int> { return 2; } async function wrapping_exceptions(): Awaitable<void> { $handles = vec[ HH\Asio\wrap(exception_thrower()), HH\Asio\wrap(non_exception_thrower()), ]; // Since we wrapped, the results will contain both the exception and the // integer result $results = await Vec\from_async($handles); var_dump($results); } <<__EntryPoint>> function main(): void { HH\Asio\join(wrapping_exceptions()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/10-exceptions-04.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/10-exceptions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<__Memoize>> async function throw_something(): Awaitable<int> { throw new Exception(); } async function foo(): Awaitable<void> { await throw_something(); } async function bar(): Awaitable<void> { await throw_something(); } <<__EntryPoint>> async function main(): Awaitable<void> { try { await foo(); } catch (Exception $e) { var_dump($e->getTrace()[2] as shape('function' => string, ...)['function']); } try { await bar(); } catch (Exception $e) { var_dump($e->getTrace()[2] as shape('function' => string, ...)['function']); } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/13-async-blocks-01.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/13-async-blocks.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function get_int_async(): Awaitable<int> { return 4; } async function get_float_async(): Awaitable<float> { return 1.2; } async function get_string_async(): Awaitable<string> { return "Hello"; } async function call_async<Tv>((function(): Awaitable<Tv>) $gen): Awaitable<Tv> { return await $gen(); } async function use_async_lambda(): Awaitable<void> { // To use an async lambda with no arguments, you would need to have a helper // function to return an actual Awaitable for you. $x = await call_async( async () ==> { $y = await get_float_async(); $z = await get_int_async(); return \round($y) + $z; }, ); \var_dump($x); } async function use_async_block(): Awaitable<void> { // With an async block, no helper function is needed. It is all built-in to the // async block itself. $x = await async { $y = await get_float_async(); $z = await get_int_async(); return \round($y) + $z; }; \var_dump($x); } async function call_async_function(): Awaitable<void> { // Normally we have to call a simple async function and get its value, even // if it takes no arguments, etc. $x = await get_string_async(); \var_dump($x); } async function use_async_block_2(): Awaitable<void> { // Here we can inline our function right in the async block $x = await async { return "Hello"; }; \var_dump($x); } <<__EntryPoint>> function main(): void { \HH\Asio\join(use_async_lambda()); \HH\Asio\join(use_async_block()); \HH\Asio\join(call_async_function()); \HH\Asio\join(use_async_block_2()); }
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/15-await-as-an-expression-01.hack_error
// @generated by hh_manual from manual/hack/15-asynchronous-operations/15-await-as-an-expression.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> { // Syntax error. $y = await foo_async(await bar_async()); // Must be written as this instead. $x = await bar_async(); $y = await foo_async($x); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/19-extensions-01.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/19-extensions.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function get_resources(): vec<resource> { $r1 = fopen('php://stdout', 'w'); $r2 = fopen('php://stdout', 'w'); $r3 = fopen('php://stdout', 'w'); return vec[$r1, $r2, $r3]; } async function write_all(vec<resource> $resources): Awaitable<void> { $write_single_resource = async function(resource $r) { $status = await stream_await($r, STREAM_AWAIT_WRITE, 1.0); if ($status === STREAM_AWAIT_READY) { fwrite($r, str_shuffle('ABCDEF').\PHP_EOL); } }; // You will get 3 shuffled strings, each on a separate line. await Vec\from_async(\array_map($write_single_resource, $resources)); } <<__EntryPoint>> function main(): void { HH\Asio\join(write_all(get_resources())); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/22-generators-01.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/22-generators.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ const int SECOND = 1000000; // microseconds async function countdown(int $from): AsyncIterator<int> { for ($i = $from; $i >= 0; --$i) { await \HH\Asio\usleep(SECOND); // Every second, a value will be yielded back to the caller, happy_new_year() yield $i; } } async function happy_new_year(int $start): Awaitable<void> { // Get the AsyncIterator that enables the countdown $ait = countdown($start); foreach ($ait await as $time) { // we are awaiting the returned awaitable, so this will be an int if ($time > 0) { echo $time."\n"; } else { echo "HAPPY NEW YEAR!!!\n"; } } } <<__EntryPoint>> function run(): void { \HH\Asio\join(happy_new_year(5)); // 5 second countdown }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/22-generators-02.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/22-generators.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ const int HALF_SECOND = 500000; // microseconds async function get_name_string(int $id): Awaitable<string> { // simulate fetch to database where we would actually use $id await \HH\Asio\usleep(HALF_SECOND); return \str_shuffle("ABCDEFG"); } async function generate(): AsyncGenerator<int, string, int> { $id = yield 0 => ''; // initialize $id // $id is a ?int; you can pass null to send() while ($id is nonnull) { $name = await get_name_string($id); $id = yield $id => $name; // key/string pair } } async function associate_ids_to_names(vec<int> $ids): Awaitable<void> { $async_generator = generate(); // You have to call next() before you send. So this is the priming step and // you will get the initialization result from generate() $result = await $async_generator->next(); \var_dump($result); foreach ($ids as $id) { // $result will be an array of ?int and string $result = await $async_generator->send($id); \var_dump($result); } } <<__EntryPoint>> function run(): void { $ids = vec[1, 2, 3, 4]; \HH\Asio\join(associate_ids_to_names($ids)); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/22-generators-03.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/22-generators.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ const int HALF_SECOND = 500000; // microseconds async function get_name_string(int $id): Awaitable<string> { // simulate fetch to database where we would actually use $id await \HH\Asio\usleep(HALF_SECOND); return \str_shuffle("ABCDEFG"); } async function generate(): AsyncGenerator<int, string, int> { $id = yield 0 => ''; // initialize $id // $id is a ?int; you can pass null to send() while ($id is nonnull) { $name = ""; try { $name = await get_name_string($id); $id = yield $id => $name; // key/string pair } catch (\Exception $ex) { \var_dump($ex->getMessage()); $id = yield 0 => ''; } } } async function associate_ids_to_names(vec<int> $ids): Awaitable<void> { $async_generator = generate(); // You have to call next() before you send. So this is the priming step and // you will get the initialization result from generate() $result = await $async_generator->next(); \var_dump($result); foreach ($ids as $id) { if ($id === 3) { $result = await $async_generator->raise( new \Exception("Id of 3 is bad!"), ); } else { $result = await $async_generator->send($id); } \var_dump($result); } } <<__EntryPoint>> function run(): void { $ids = vec[1, 2, 3, 4]; \HH\Asio\join(associate_ids_to_names($ids)); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-01.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function get_hello(): string { return "Hello"; } <<__EntryPoint>> function run_na_hello(): void { \var_dump(get_hello()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-02.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function get_hello(): Awaitable<string> { return "Hello"; } <<__EntryPoint>> async function run_a_hello(): Awaitable<void> { $x = await get_hello(); \var_dump($x); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-03.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class User { public string $name; protected function __construct(string $name) { $this->name = $name; } public static function get_name(int $id): User { return new User(\str_shuffle("ABCDEFGHIJ").\strval($id)); } } async function load_user(int $id): Awaitable<User> { // Load user from somewhere (e.g., database). // Fake it for now return User::get_name($id); } async function load_users_await_loop(vec<int> $ids): Awaitable<vec<User>> { $result = vec[]; foreach ($ids as $id) { $result[] = await load_user($id); } return $result; } <<__EntryPoint>> function runMe(): void { $ids = vec[1, 2, 5, 99, 332]; $result = \HH\Asio\join(load_users_await_loop($ids)); \var_dump($result[4]->name); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-04.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class User { public string $name; protected function __construct(string $name) { $this->name = $name; } public static function get_name(int $id): User { return new User(\str_shuffle("ABCDEFGHIJ").\strval($id)); } } async function load_user(int $id): Awaitable<User> { // Load user from somewhere (e.g., database). // Fake it for now return User::get_name($id); } async function load_users_no_loop(vec<int> $ids): Awaitable<vec<User>> { return await Vec\map_async( $ids, async $id ==> await load_user($id), ); } <<__EntryPoint>> function runMe(): void { $ids = vec[1, 2, 5, 99, 332]; $result = \HH\Asio\join(load_users_no_loop($ids)); \var_dump($result[4]->name); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-05.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class PostData { // using constructor argument promotion public function __construct(public string $text) {} } async function fetch_all_post_ids_for_author( int $author_id, ): Awaitable<vec<int>> { // Query database, etc., but for now, just return made up stuff return vec[4, 53, 99]; } async function fetch_post_data(int $post_id): Awaitable<PostData> { // Query database, etc. but for now, return something random return new PostData(\str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); } async function fetch_comment_count(int $post_id): Awaitable<int> { // Query database, etc., but for now, return something random return \rand(0, 50); } async function fetch_page_data( int $author_id, ): Awaitable<vec<(PostData, int)>> { $all_post_ids = await fetch_all_post_ids_for_author($author_id); // An async closure that will turn a post ID into a tuple of // post data and comment count $post_fetcher = async function(int $post_id): Awaitable<(PostData, int)> { list($post_data, $comment_count) = await Vec\from_async(vec[ fetch_post_data($post_id), fetch_comment_count($post_id), ]); invariant($post_data is PostData, "This is good"); invariant($comment_count is int, "This is good"); return tuple($post_data, $comment_count); }; // Transform the array of post IDs into an vec of results, // using the Vec\map_async function return await Vec\map_async($all_post_ids, $post_fetcher); } async function generate_page(int $author_id): Awaitable<string> { $tuples = await fetch_page_data($author_id); $page = ""; foreach ($tuples as $tuple) { list($post_data, $comment_count) = $tuple; // Normally render the data into HTML, but for now, just create a // normal string $page .= $post_data->text." ".$comment_count.\PHP_EOL; } return $page; } <<__EntryPoint>> function main(): void { print \HH\Asio\join(generate_page(13324)); // just made up a user id }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-06.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function b_one(string $key): Awaitable<string> { $subkey = await Batcher::lookup($key); return await Batcher::lookup($subkey); } async function b_two(string $key): Awaitable<string> { return await Batcher::lookup($key); } async function batching(): Awaitable<void> { $results = await Vec\from_async(vec[b_one('hello'), b_two('world')]); \printf("%s\n%s\n", $results[0], $results[1]); } <<__EntryPoint>> function main(): void { HH\Asio\join(batching()); } class Batcher { private static vec<string> $pendingKeys = vec[]; private static ?Awaitable<dict<string, string>> $aw = null; public static async function lookup(string $key): Awaitable<string> { // Add this key to the pending batch self::$pendingKeys[] = $key; // If there's no awaitable about to start, create a new one if (self::$aw === null) { self::$aw = self::go(); } // Wait for the batch to complete, and get our result from it $results = await self::$aw; return $results[$key]; } private static async function go(): Awaitable<dict<string, string>> { // Let other awaitables get into this batch await \HH\Asio\later(); // Now this batch has started; clear the shared state $keys = self::$pendingKeys; self::$pendingKeys = vec[]; self::$aw = null; // Do the multi-key roundtrip return await multi_key_lookup($keys); } } async function multi_key_lookup( vec<string> $keys, ): Awaitable<dict<string, string>> { // lookup multiple keys, but, for now, return something random $r = dict[]; foreach ($keys as $key) { $r[$key] = \str_shuffle("ABCDEF"); } return $r; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-07.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function speak(): Awaitable<void> { echo "one"; await \HH\Asio\later(); echo "two"; echo "three"; } <<__EntryPoint>> async function forget_await(): Awaitable<void> { $handle = speak(); // This just gets you the handle }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-08.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function get_curl_data(string $url): Awaitable<string> { return await \HH\Asio\curl_exec($url); } function possible_side_effects(): int { \sleep(1); echo "Output buffer stuff"; return 4; } async function proximity(): Awaitable<void> { $handle = get_curl_data("http://example.com"); possible_side_effects(); await $handle; // instead you should await get_curl_data("....") here } <<__EntryPoint>> function main(): void { \HH\Asio\join(proximity()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-09.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract final class MemoizeResult { private static async function time_consuming(): Awaitable<string> { await \HH\Asio\usleep(5000000); return "This really is not time consuming, but the sleep fakes it."; } private static ?string $result = null; public static async function memoize_result(): Awaitable<string> { if (self::$result === null) { self::$result = await self::time_consuming(); // don't memoize the resulting data } return self::$result; } } <<__EntryPoint>> function runMe(): void { $t1 = \microtime(true); \HH\Asio\join(MemoizeResult::memoize_result()); $t2 = \microtime(true) - $t1; $t3 = \microtime(true); \HH\Asio\join(MemoizeResult::memoize_result()); $t4 = \microtime(true) - $t3; \var_dump($t4 < $t2); // The memoized result will get here a lot faster }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-10.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ abstract final class MemoizeAwaitable { private static async function time_consuming(): Awaitable<string> { await \HH\Asio\usleep(5000000); return "Not really time consuming but sleep."; // For type-checking purposes } private static ?Awaitable<string> $handle = null; public static function memoize_handle(): Awaitable<string> { if (self::$handle === null) { self::$handle = self::time_consuming(); // memoize the awaitable } return self::$handle; } } <<__EntryPoint>> function runMe(): void { $t1 = \microtime(true); \HH\Asio\join(MemoizeAwaitable::memoize_handle()); $t2 = \microtime(true) - $t1; $t3 = \microtime(true); \HH\Asio\join(MemoizeAwaitable::memoize_handle()); $t4 = \microtime(true) - $t3; \var_dump($t4 < $t2); // The memoized result will get here a lot faster }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-11.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function fourth_root(num $n): Awaitable<float> { return sqrt(sqrt((float)$n)); } async function normal_call(): Awaitable<vec<float>> { $nums = vec[64, 81]; return await Vec\map_async($nums, fourth_root<>); } async function closure_call(): Awaitable<vec<float>> { $nums = vec[64, 81]; $froots = async function(num $n): Awaitable<float> { return sqrt(sqrt((float)$n)); }; return await Vec\map_async($nums, $froots); } async function lambda_call(): Awaitable<vec<float>> { $nums = vec[64, 81]; return await Vec\map_async($nums, async $num ==> sqrt(sqrt((float)$num))); } async function use_lambdas(): Awaitable<void> { $nc = await normal_call(); $cc = await closure_call(); $lc = await lambda_call(); \var_dump($nc); \var_dump($cc); \var_dump($lc); } <<__EntryPoint>> function main(): void { HH\Asio\join(use_lambdas()); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/28-guidelines-12.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/28-guidelines.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function join_async(): Awaitable<string> { return "Hello"; } // In an async function, you would await an awaitable. // In a non-async function, or the global scope, you can // use `join` to force the the awaitable to run to its completion. <<__EntryPoint>> function main(): void { $s = \HH\Asio\join(join_async()); \var_dump($s); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/31-examples-01.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/31-examples.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // async specifies a function will return an awaitable. Awaitable<string> means // that the awaitable will ultimately return a string when complete async function trivial(): Awaitable<string> { return "Hello"; } <<__EntryPoint>> async function call_trivial(): Awaitable<void> { // These first two lines could be combined into // $result = await trivial(); // but wanted to show the process // get awaitable that you can wait for completion $aw = trivial(); // wait for the operation to complete and get the result $result = await $aw; echo $result; // "Hello" }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/31-examples-02.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/31-examples.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function join_async(): Awaitable<string> { return "Hello"; } // In an async function, you would await an awaitable. // In a non-async function, or the global scope, you can // use `join` to force the the awaitable to run to its completion. <<__EntryPoint>> function main(): void { $s = \HH\Asio\join(join_async()); \var_dump($s); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/31-examples-03.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/31-examples.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<__EntryPoint>> async function closure_async(): Awaitable<void> { // closure $hello = async function(): Awaitable<string> { return 'Hello'; }; // lambda $bye = async ($str) ==> $str; // The call style to either closure or lambda is the same $rh = await $hello(); $rb = await $bye("bye"); echo $rh." ".$rb.\PHP_EOL; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/31-examples-04.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/31-examples.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class PostData { // using constructor argument promotion public function __construct(public string $text) {} } async function fetch_all_post_ids_for_author( int $author_id, ): Awaitable<vec<int>> { // Query database, etc., but for now, just return made up stuff return vec[4, 53, 99]; } async function fetch_post_data(int $post_id): Awaitable<PostData> { // Query database, etc. but for now, return something random return new PostData(\str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); } async function fetch_comment_count(int $post_id): Awaitable<int> { // Query database, etc., but for now, return something random return \rand(0, 50); } async function fetch_page_data( int $author_id, ): Awaitable<vec<(PostData, int)>> { $all_post_ids = await fetch_all_post_ids_for_author($author_id); // An async closure that will turn a post ID into a tuple of // post data and comment count $post_fetcher = async function(int $post_id): Awaitable<(PostData, int)> { concurrent { $post_data = await fetch_post_data($post_id); $comment_count = await fetch_comment_count($post_id); } return tuple($post_data, $comment_count); // alternatively: $_return = tuple( await fetch_post_data($post_id), await fetch_comment_count($post_id), ); }; // Transform the array of post IDs into a vec of results, // using the Vec\map_async function return await Vec\map_async($all_post_ids, $post_fetcher); } async function generate_page(int $author_id): Awaitable<string> { $tuples = await fetch_page_data($author_id); $page = ""; foreach ($tuples as $tuple) { list($post_data, $comment_count) = $tuple; // Normally render the data into HTML, but for now, just create a // normal string $page .= $post_data->text." ".$comment_count.\PHP_EOL; } return $page; } <<__EntryPoint>> function main(): void { print \HH\Asio\join(generate_page(13324)); // just made up a user id }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/31-examples-05.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/31-examples.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ async function b_one(string $key): Awaitable<string> { $subkey = await Batcher::lookup($key); return await Batcher::lookup($subkey); } async function b_two(string $key): Awaitable<string> { return await Batcher::lookup($key); } async function batching(): Awaitable<void> { $results = await Vec\from_async(vec[b_one('hello'), b_two('world')]); \printf("%s\n%s\n", $results[0], $results[1]); } <<__EntryPoint>> function main(): void { \HH\Asio\join(batching()); } class Batcher { private static vec<string> $pendingKeys = vec[]; private static ?Awaitable<dict<string, string>> $aw = null; public static async function lookup(string $key): Awaitable<string> { // Add this key to the pending batch self::$pendingKeys[] = $key; // If there's no awaitable about to start, create a new one if (self::$aw === null) { self::$aw = self::go(); } // Wait for the batch to complete, and get our result from it $results = await self::$aw; return $results[$key]; } private static async function go(): Awaitable<dict<string, string>> { // Let other awaitables get into this batch await \HH\Asio\later(); // Now this batch has started; clear the shared state $keys = self::$pendingKeys; self::$pendingKeys = vec[]; self::$aw = null; // Do the multi-key roundtrip return await multi_key_lookup($keys); } } async function multi_key_lookup( vec<string> $keys, ): Awaitable<dict<string, string>> { // lookup multiple keys, but, for now, return something random $r = dict[]; foreach ($keys as $key) { $r[$key] = \str_shuffle("ABCDEF"); } return $r; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/15-asynchronous-operations/31-examples-06.hack
// @generated by hh_manual from manual/hack/15-asynchronous-operations/31-examples.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ // Of course, this is all made up :) class Polling { private int $count = 0; public function isReady(): bool { $this->count++; if ($this->count > 10) { return true; } return false; } public function getResult(): int { return 23; } } async function do_polling(Polling $p): Awaitable<int> { echo "do polling 1".\PHP_EOL; // No async function in Polling, so loop until we are ready, but let // other awaitables go via later() while (!$p->isReady()) { await \HH\Asio\later(); } echo "\ndo polling 2".\PHP_EOL; return $p->getResult(); } async function no_polling(): Awaitable<string> { echo '.'; return \str_shuffle("ABCDEFGH"); } async function polling_example(): Awaitable<void> { $handles = vec[do_polling(new Polling())]; // To make this semi-realistic, call no_polling a bunch of times to show // that do_polling is waiting. for ($i = 0; $i < 50; $i++) { $handles[] = no_polling(); } $results = await Vec\from_async($handles); } <<__EntryPoint>> function main(): void { \HH\Asio\join(polling_example()); }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/01-introduction-01.hack_error
// @generated by hh_manual from manual/hack/16-readonly/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Bar { public function __construct( public Foo $foo, ){} } class Foo { public function __construct( public int $prop, ) {} } function test(readonly Foo $x) : void { $x->prop = 4; // error, $x is readonly, its properties cannot be modified }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/01-introduction-02.hack_error
// @generated by hh_manual from manual/hack/16-readonly/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function test(readonly Bar $x) : void { $foo = $x->foo; $foo->prop = 3; // error, $foo is readonly }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/02-syntax-01.hack
// @generated by hh_manual from manual/hack/16-readonly/02-syntax.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Bar { public function __construct( public Foo $foo, ){} } class Foo { public function __construct( public int $prop, ) {} } function getFoo(readonly Bar $x): readonly Foo { return $x->foo; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/02-syntax-02.hack
// @generated by hh_manual from manual/hack/16-readonly/02-syntax.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Bar {} class Foo { private static readonly ?Bar $static_bar = null; public function __construct( private readonly Bar $bar, ){} }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/02-syntax-03.hack
// @generated by hh_manual from manual/hack/16-readonly/02-syntax.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Bar {} function call( (function(readonly Bar) : readonly Bar) $f, readonly Bar $arg, ) : readonly Bar { return readonly $f($arg); }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/02-syntax-04.hack
// @generated by hh_manual from manual/hack/16-readonly/02-syntax.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo {} function foo(): void { $x = new Foo(); $y = readonly $x; }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/02-syntax-05.hack_error
// @generated by hh_manual from manual/hack/16-readonly/02-syntax.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class C { public function __construct(public int $prop) {} public readonly function foo() : void { $this->prop = 4; // error, $this is readonly. } }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/02-syntax-06.hack_error
// @generated by hh_manual from manual/hack/16-readonly/02-syntax.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Data {} class Box { public function __construct(public Data $data) {} public readonly function getData(): readonly Data { return $this->data; } public function setData(Data $d) : void { $this->data = $d; } } function readonly_method_example(readonly Box $b): void { $y = readonly $b->getData(); // ok, $y is readonly $b->setData(new Data()); // error, $b is readonly, it can only call readonly methods }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/02-syntax-07.hack_error
// @generated by hh_manual from manual/hack/16-readonly/02-syntax.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function readonly_closure_example(): void { $x = new Foo(); $f = readonly () ==> { $x->prop = 4; // error, $x is readonly here! }; }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/03-subtyping-01.hack_error
// @generated by hh_manual from manual/hack/16-readonly/03-subtyping.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo { public int $prop = 0; } function takes_mutable(Foo $x): void { $x->prop = 4; } function test(): void { $z = readonly new Foo(); takes_mutable($z); // error, takes_mutable's first parameter // is mutable, but $z is readonly }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/03-subtyping-02.hack_error
// @generated by hh_manual from manual/hack/16-readonly/03-subtyping.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo {} function returns_mutable(readonly Foo $x): Foo { return $x; // error, $x is readonly } function returns_readonly(readonly Foo $x): readonly Foo { return $x; // correct }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/03-subtyping-03.hack
// @generated by hh_manual from manual/hack/16-readonly/03-subtyping.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo {} // promises not to modify $x function takes_readonly(readonly Foo $x): void { } function test(): void { $z = new Foo(); takes_readonly($z); // ok }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/03-subtyping-04.hack_error
// @generated by hh_manual from manual/hack/16-readonly/03-subtyping.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Bar {} class Foo { public function __construct( public readonly Bar $ro_prop, public Bar $mut_prop ){} } function test( Foo $x, readonly Bar $bar, ) : void { $x->mut_prop = $bar; // error, $bar is readonly but the prop is mutable $x->ro_prop = $bar; // ok }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/04-explicit-readonly-keywords-01.hack
// @generated by hh_manual from manual/hack/16-readonly/04-explicit-readonly-keywords.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo {} function returns_readonly(): readonly Foo { return readonly new Foo(); } function test(): void { $x = readonly returns_readonly(); // this is required to call returns_readonly() }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/04-explicit-readonly-keywords-02.hack
// @generated by hh_manual from manual/hack/16-readonly/04-explicit-readonly-keywords.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Bar {} class Foo { public function __construct( public readonly Bar $bar, ) {} } function test(Foo $f): void { $bar = readonly $f->bar; // this is required }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/04-explicit-readonly-keywords-03.hack
// @generated by hh_manual from manual/hack/16-readonly/04-explicit-readonly-keywords.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<file:__EnableUnstableFeatures("readonly")>> class Bar {} class Foo { public static readonly ?Bar $bar = null; } function read_static()[read_globals]: void { $y = readonly Foo::$bar; // keyword required } function read_static2()[leak_safe]: void { $y = readonly Foo::$bar; // keyword required }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/05-containers-and-collections-01.hack_error
// @generated by hh_manual from manual/hack/16-readonly/05-containers-and-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo {} function container_example(readonly Foo $x) : void { $container = vec[]; $container[] = $x; // error, $x is readonly }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/05-containers-and-collections-02.hack
// @generated by hh_manual from manual/hack/16-readonly/05-containers-and-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo {} function container_example2(readonly Foo $x) : void { $container = readonly vec[]; // container is now a readonly vec $container_literal = vec[new Foo(), readonly new Foo()]; // $container_literal is readonly }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/05-containers-and-collections-03.hack_error
// @generated by hh_manual from manual/hack/16-readonly/05-containers-and-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo { public function __construct(public int $prop) {} } function container_foreach(readonly vec<Foo> $vec): void { foreach($vec as $elem) { $elem->prop = 3; // error, $elem is readonly } }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/05-containers-and-collections-04.hack_error
// @generated by hh_manual from manual/hack/16-readonly/05-containers-and-collections.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo {} function collection_example(): void { $v = Vector { new Foo(), readonly new Foo() }; // $v is readonly since at least one of its contents is readonly $v[] = new Foo(); // error, $v is readonly and not a value type, so it cannot be appended }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/06-advanced-semantics-01.hack
// @generated by hh_manual from manual/hack/16-readonly/06-advanced-semantics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function readonly_closures_example2<T>( (function (): T) $regular_f, (readonly function(): T) $ro_f, ) : void { $ro_regular_f = readonly $regular_f; // readonly (function(): T) $ro_f; // (readonly function(): T) $ro_ro_f = readonly $ro_f; // readonly (readonly function(): T) }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/06-advanced-semantics-02.hack_error
// @generated by hh_manual from manual/hack/16-readonly/06-advanced-semantics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function readonly_closure_call<T>( (function (): T) $regular_f, (readonly function(): T) $ro_f, ) : void { $ro_regular_f = readonly $regular_f; // readonly (function(): T) $ro_regular_f(); // error, $ro_regular_f is a readonly reference to a regular function }
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/06-advanced-semantics-03.hack_error
// @generated by hh_manual from manual/hack/16-readonly/06-advanced-semantics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ function readonly_closure_call2<T>( (function (): T) $regular_f, (readonly function(): T) $ro_f, ) : void { $ro_regular_f = readonly $regular_f; // readonly (function(): T) $ro_regular_f(); // error, $ro_regular_f is a readonly reference to a regular function $ro_ro_f = readonly $ro_f; // readonly (readonly function(): T) $ro_ro_f(); // safe }
Hack
hhvm/hphp/hack/test/extracted_from_manual/16-readonly/06-advanced-semantics-04.hack
// @generated by hh_manual from manual/hack/16-readonly/06-advanced-semantics.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Foo { public function __construct( public int $prop, ) {} public readonly function get() : int { $result = $this->prop; // here, $result is readonly, but its also an int. return \HH\Readonly\as_mut($this->prop); // convert to a non-readonly value } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/01-introduction-01.hack
// @generated by hh_manual from manual/hack/17-modules/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {} new module foo.bar {} new module foo.bar.test {}
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/01-introduction-foomodule.hack
//// module.hack // @generated by hh_manual from manual/hack/17-modules/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {} //// foo_class.hack module foo; public class Foo {} internal class FooInternal { public function foo(): void {} internal function bar(): void {} } internal function foo_fun(): void {}
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/02-defining-modules-01.hack
// @generated by hh_manual from manual/hack/17-modules/02-defining-modules.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {}
hhvm/hphp/hack/test/extracted_from_manual/17-modules/02-defining-modules-02.hack_error
// @generated by hh_manual from manual/hack/17-modules/02-defining-modules.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ module foo; // ok module bar; // not okay: duplicate module membership statement
hhvm/hphp/hack/test/extracted_from_manual/17-modules/02-defining-modules-03.hack_error
// @generated by hh_manual from manual/hack/17-modules/02-defining-modules.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ module bar; // not okay: module definitions must live outside of files already in a module new module foo {}
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/04-inheritance-01.hack
//// newmodule.hack // @generated by hh_manual from manual/hack/17-modules/04-inheritance.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {} //// foo.hack module foo; internal interface IFoo { //... } class Foo implements IFoo {} // ok
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/05-traits-01.hack
//// newmodule.hack // @generated by hh_manual from manual/hack/17-modules/05-traits.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {} //// foo.hack module foo; internal trait TFoo { public function foo(): void {} internal function bar(): void {} }
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/06-type-aliases-01.hack
//// newmodule.hack // @generated by hh_manual from manual/hack/17-modules/06-type-aliases.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {} //// foo.hack module foo; internal class Foo {} internal type FooInternal = Foo; internal newtype FooOpaque = FooInternal; internal newtype FooOpaque2 as Foo = Foo;
hhvm/hphp/hack/test/extracted_from_manual/17-modules/06-type-aliases-02.hack_error
// @generated by hh_manual from manual/hack/17-modules/06-type-aliases.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ internal module newtype FooModuleErr2 = int; // Parse error
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/07-closures-and-function-pointers-01.hack
//// newmodule.hack // @generated by hh_manual from manual/hack/17-modules/07-closures-and-function-pointers.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {} //// foo.hack module foo; internal function f() : void { echo "Internal f\n"; } public function getF(): (function():void) { return f<>; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/07-closures-and-function-pointers-02.hack
//// newmodule.hack // @generated by hh_manual from manual/hack/17-modules/07-closures-and-function-pointers.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {} //// foo.hack module foo; internal function f() : void { echo "Internal f\n"; } public function getF(): (function():void) { return () ==> { f(); }; // ok }
Hack
hhvm/hphp/hack/test/extracted_from_manual/17-modules/08-reflection-and-migration-01.hack
//// newmodule.hack // @generated by hh_manual from manual/hack/17-modules/08-reflection-and-migration.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ new module foo {} //// foo.hack module foo; class Cls { <<__SoftInternal>> internal function foo_soft(): void { echo "Hello from foo_soft\n"; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/20-attributes/01-introduction-01.hack
// @generated by hh_manual from manual/hack/20-attributes/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<__Memoize>> function add_one(int $x): int { return $x + 1; }
Hack
hhvm/hphp/hack/test/extracted_from_manual/20-attributes/01-introduction-02.hack
// @generated by hh_manual from manual/hack/20-attributes/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ <<__ConsistentConstruct>> class OtherClass { <<__Memoize, __Deprecated("Use FooClass methods instead")>> public function addOne(int $x): int { return $x + 1; } }
Hack
hhvm/hphp/hack/test/extracted_from_manual/20-attributes/01-introduction-contributors.hack
// @generated by hh_manual from manual/hack/20-attributes/01-introduction.md // @codegen-command : buck run fbcode//hphp/hack/src/hh_manual:hh_manual extract fbcode/hphp/hack/manual/hack/ class Contributors implements HH\ClassAttribute { public function __construct(private string $author, private ?keyset<string> $maintainers = null) {} public function getAuthor(): string { return $this->author; } public function getMaintainers(): keyset<string> { return $this->maintainers ?? keyset[$this->author]; } } <<Contributors("John Doe", keyset["ORM Team", "Core Library Team"])>> class MyClass {} <<Contributors("You")>> class YourClass {} async function example_snippet_wrapper1(): Awaitable<void> { $rc = new ReflectionClass('MyClass'); $my_class_contributors = $rc->getAttributeClass(Contributors::class); $my_class_contributors?->getAuthor(); // "John Doe" $my_class_contributors?->getMaintainers(); // keyset["ORM Team", "Core Library Team"] }