Last active
May 27, 2021 12:54
-
-
Save azjezz/b50c809e2aba2c570b3e36fde6b8dd81 to your computer and use it in GitHub Desktop.
Context and CoEffects in PHP: https://github.com/facebook/hhvm/blob/master/hphp/hack/doc/HIPs/contexts_and_coeffects.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @context={} | |
*/ | |
function foo(): void {} | |
/** | |
* @context={io} | |
*/ | |
function bar(): void { echo 'bar'; } | |
/** | |
* @context={rand} | |
*/ | |
function baz(): int { return mt_rand(1, 10); } | |
/** | |
* @context={io, rand} | |
*/ | |
function qux(): void { echo baz(); } | |
/** | |
* @template Tk of array-key | |
* @template Tv | |
* @template Ts | |
* | |
* @context-template C1 | |
* @context-template C2 | |
* | |
* @param (iterable<Tk, Tv>){C1} $iterable | |
* @param (callable(Tk): Ts){C2} $fun | |
* | |
* @return array<Tk, Ts> | |
* | |
* @context={...C1, ...C2} | |
*/ | |
function map(iterable $iterable, callable $fun): array | |
{ | |
$r = []; | |
foreach($iterable as $k => $i) { $r[$k] = $i; } | |
return $r; | |
} | |
/** | |
* this function is pure, so it has no effects ( {} ) | |
* | |
* @context={} | |
*/ | |
function main(): void | |
{ | |
map( | |
[1, 2], | |
/** | |
* @context={} | |
*/ | |
static fn(int $i): int => $i + 1 | |
); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment