hacklang - 如何访问 hack 中的可选形状字段?

标签 hacklang

举个例子,假设我有一个如下形状的类型 foo

type foo = shape(
  ?'bar' => float,
  ...
);

现在,如果我尝试通过以下方式访问字段 bar 的值,

do_something_with($rcvd['bar']);

其中 $rcvdfoo 类型,它不起作用,因为 bar 是可选成员并且可能不存在于实例中$rcvd。 所以对于这个给定的例子,问题是 - 如何访问 $rcvd 的成员 bar

最佳答案

好的,找到了:https://docs.hhvm.com/hack/reference/class/HH.Shapes/idx/

所以正确的做法是,

Shapes::idx($rcvd, 'bar');

关于hacklang - 如何访问 hack 中的可选形状字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49304646/

相关文章:

casting - Hacklang — 可以使用类型常量的类型约束吗?

ubuntu - 如何使用 Atom/Nuclide 和 Hack 进行内联错误检查?

php - 在 HHVM 上用 Hack 编写的代码是否比用 PHP 编写的代码快?

Php Hack中使用反射获取泛型类型

Hacklang:如何从异常中获取堆栈跟踪?

hacklang - hack中常用的php函数

php - Codeigniter 和黑客语言

hacklang - 如何修复这个 HH FIXME [4410]?

data-structures - 我什么时候会在 PHP 数组上使用 Hack dict?

c++ - Centos 7安装HHVM报错"Internal compiler"