博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hack语言类型化简介
阅读量:5312 次
发布时间:2019-06-14

本文共 3076 字,大约阅读时间需要 10 分钟。

在typechecker的配合下,Hack语言的类型化能力是Hack其他功能特性的基石。开发Hack语言的主要动机也正是为代码提供显式类型标注以便对代码进行类型一致性和潜在错误分析。

这是用于对比Hack特性的一个实例,用传统PHP形式编写:

a = $a; $this->b = $b; } public function foo($x, $y) { return $x * $this->a + $y * $this->b; }}function bar(A $a, $x, $y) { return $a->foo($x, $y);}function baz() { $a = new A(2, 4); $z = new Z(); var_dump(bar($a, 9, 4)); // Did we really want to allow passing a stringy int? var_dump(bar($a, 8, "3")); // Did we really want to allow passing booleans? var_dump(bar($a, true, false)); // This will throw a fatal at runtime var_dump(bar($z, 1, 1));}baz();Outputint(34)int(28)int(2)Catchable fatal error: Argument 1 passed to Hack\UserDocumentation\Types\Intro\Examples\PreHack\bar() must be an instance of Hack\UserDocumentation\Types\Intro\Examples\PreHack\A, Hack\UserDocumentation\Types\Intro\Examples\PreHack\Z given in /data/users/joelm/user-documentation/guides/hack/20-types/01-introduction-examples/pre-hack.php on line 22

上述示例可以完美运行于HHVM上(除了在最后的var_dump处发生一个致命错误)。尽管如此,在很多方面它没有清晰的表达出开发者的意图。比如,开发者是否允许A::foo()接受字符串类型的int值。当然,习惯上的处理方式,如通过is_int()函数来检测或抛出异常可以缓解这种情况。

但是你看看下面的例子,开发者的意图是否更清晰:

a = $a; $this->b = $b; } public function foo(int $x, int $y): int { return $x * $this->a + $y * $this->b; }}function bar(A $a, int $x, int $y): int { return $a->foo($x, $y);}function baz(): void { $a = new A(2, 4); $z = new Z(); var_dump(bar($a, 9, 4)); // Did we really want to allow passing a stringy int? NO! // The typechecker will actually error here before you even run the program, // so you can catch problems before runtime. var_dump(bar($a, 8, "3")); // Did we really want to allow passing booleans? NO! // The typechecker will error here too. var_dump(bar($a, true, false)); // This will throw a fatal at runtime // The typechecker will error here as well var_dump(bar($z, 1, 1));}baz();/****Type checker errors:hack.php:29:23,25: Invalid argument (Typing[4110]) hack.php:20:28,30: This is an int hack.php:29:23,25: It is incompatible with a stringhack.php:31:20,23: Invalid argument (Typing[4110]) hack.php:20:20,22: This is an int hack.php:31:20,23: It is incompatible with a boolhack.php:31:26,30: Invalid argument (Typing[4110]) hack.php:20:28,30: This is an int hack.php:31:26,30: It is incompatible with a boolhack.php:33:16,17: Invalid argument (Typing[4110]) hack.php:20:14,14: This is an object of type Hack\UserDocumentation\Types\Intro\Examples\Hack\A hack.php:26:8,14: It is incompatible with an object of type Hack\UserDocumentation\Types\Intro\Examples\Hack\Z*****/Outputint(34)Catchable fatal error: Argument 3 passed to Hack\UserDocumentation\Types\Intro\Examples\Hack\bar() must be an instance of int, string given in /data/users/joelm/user-documentation/guides/hack/20-types/01-introduction-examples/hack.php.type-errors on line 22

看到这个例子里的代码,我们可以很清晰的知道仅允许传递int型值。这样API的调用者知道需要传递什么类型的值。为方法和属性添加显式类型声明,配合Hack typechecker,你就有了一款真正的很强的安全基础的动态编程语言。

要想了解代码中可以使用哪些类型以及在哪纺织显式类型标注,查看:

  • 类型系统
  • 类型标注

转载于:https://www.cnblogs.com/x3d/p/5472222.html

你可能感兴趣的文章
hdu 4541(麻烦的模拟)
查看>>
关于外存索引
查看>>
PHP抽象类与接口的区别
查看>>
Git_初步了解
查看>>
详解在中国最成功的连锁餐饮企业——百胜集团
查看>>
Spring Boot Autowirted注入找不到Bean对象解决方法
查看>>
Linux AUFS 文件系统
查看>>
个人冲刺
查看>>
Python__接口与归一化设计
查看>>
使用Bezier Paths[转]
查看>>
jQuery easyui 扩展form插件的三个方法
查看>>
typedef的用法总结
查看>>
SQL Server Cast and Convert
查看>>
[转载]由浅入深理解索引的实现(2)
查看>>
gnome 3.8更新 让人失望
查看>>
模块相关
查看>>
java动态生成验证码图片
查看>>
WIN CE和电脑之间的文件拷贝(1) Form1.Designer.cs文件
查看>>
无限循环
查看>>
集成备注
查看>>