hack形状案例
2018-11-02 17:21 更新
任意类型
形状类型可以具有任意类型的零个或多个字段。
<?hh
namespace Hack\UserDocumentation\Shapes\Examples\Examples\Arbitrary;
type Complex = shape('real' => float, 'imag' => float);
type Hits = shape('id' => string, 'url' => string, 'count' => int);
type Math = shape('n1' => array<num>,
'n2' => (function (array<num>): float));
type APoint<T> = shape('x' => T, 'y' => T);
function take_complex(Complex $c): void {
echo $c['real'] . ' + ' . $c['imag'] . 'i' . PHP_EOL;
}
function get_hits(Hits $h): void {
echo 'User with id: ' . $h['id'] . ' has hit ' . $h['url'] . ' ' .
$h['count'] . ' times.' . PHP_EOL;
}
function do_math(Math $m): void {
var_dump($m['n2']($m['n1']));
}
function create_point<T>(APoint<T> $p): void {
var_dump($p);
}
function run(): void {
take_complex(shape('real' => 4.0, 'imag' => 2.0));
get_hits(shape('id' => 'Rex', 'url' => 'http://example.com', 'count' => 10));
do_math(shape('n1' => array(2, 4.0, 2, 999, 1000.23),
'n2' => function (array<num> $nums): float {
return array_sum(array_filter($nums, $x ==> is_float($x)));
}
));
create_point(shape('x' => true, 'y' => false));
}
run();
Output
4 + 2i
User with id: Rex has hit http://example.com 10 times.
float(1004.23)
array(2) {
["x"]=>
bool(true)
["y"]=>
bool(false)
}
以上内容是否对您有帮助:
更多建议: