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

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

  1. <?php
  2. namespace Hack\UserDocumentation\Types\Intro\Examples\PreHack;
  3. class Z {}
  4. class A {
  5. public $a;
  6. public $b;
  7. public function __construct($a, $b) {
  8. $this->a = $a;
  9. $this->b = $b;
  10. }
  11. public function foo($x, $y) {
  12. return $x * $this->a + $y * $this->b;
  13. }
  14. }
  15. function bar(A $a, $x, $y) {
  16. return $a->foo($x, $y);
  17. }
  18. function baz() {
  19. $a = new A(2, 4);
  20. $z = new Z();
  21. var_dump(bar($a, 9, 4));
  22. // Did we really want to allow passing a stringy int?
  23. var_dump(bar($a, 8, "3"));
  24. // Did we really want to allow passing booleans?
  25. var_dump(bar($a, true, false));
  26. // This will throw a fatal at runtime
  27. var_dump(bar($z, 1, 1));
  28. }
  29. baz();
  30. Output
  31. int(34)
  32. int(28)
  33. int(2)
  34. 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()函数来检测或抛出异常可以缓解这种情况。

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

  1. <?hh
  2. namespace Hack\UserDocumentation\Types\Intro\Examples\Hack;
  3. class Z {}
  4. class A {
  5. public int $a;
  6. public int $b;
  7. public function __construct(int $a, int $b) {
  8. $this->a = $a;
  9. $this->b = $b;
  10. }
  11. public function foo(int $x, int $y): int {
  12. return $x * $this->a + $y * $this->b;
  13. }
  14. }
  15. function bar(A $a, int $x, int $y): int {
  16. return $a->foo($x, $y);
  17. }
  18. function baz(): void {
  19. $a = new A(2, 4);
  20. $z = new Z();
  21. var_dump(bar($a, 9, 4));
  22. // Did we really want to allow passing a stringy int? NO!
  23. // The typechecker will actually error here before you even run the program,
  24. // so you can catch problems before runtime.
  25. var_dump(bar($a, 8, "3"));
  26. // Did we really want to allow passing booleans? NO!
  27. // The typechecker will error here too.
  28. var_dump(bar($a, true, false));
  29. // This will throw a fatal at runtime
  30. // The typechecker will error here as well
  31. var_dump(bar($z, 1, 1));
  32. }
  33. baz();
  34. /****
  35. Type checker errors:
  36. hack.php:29:23,25: Invalid argument (Typing[4110])
  37. hack.php:20:28,30: This is an int
  38. hack.php:29:23,25: It is incompatible with a string
  39. hack.php:31:20,23: Invalid argument (Typing[4110])
  40. hack.php:20:20,22: This is an int
  41. hack.php:31:20,23: It is incompatible with a bool
  42. hack.php:31:26,30: Invalid argument (Typing[4110])
  43. hack.php:20:28,30: This is an int
  44. hack.php:31:26,30: It is incompatible with a bool
  45. hack.php:33:16,17: Invalid argument (Typing[4110])
  46. hack.php:20:14,14: This is an object of type
  47. Hack\UserDocumentation\Types\Intro\Examples\Hack\A
  48. hack.php:26:8,14: It is incompatible with an object of type
  49. Hack\UserDocumentation\Types\Intro\Examples\Hack\Z
  50. *****/
  51. Output
  52. int(34)
  53. 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,你就有了一款真正的很强的安全基础的动态编程语言。

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

  • 类型系统
  • 类型标注

Hack语言类型化简介的更多相关文章

  1. Facebook Hack 语言 简介

    1. Hack 是什么? Hack 是一种基于HHVM(HipHop VM 是Facebook推出的用来执行PHP代码的虚拟机,它是一个PHP的JIT编译器,同时具有产生快速代码和即时编译的优点.)的 ...

  2. Facebook的Hack语言三大看点

    Hack语言主要有三大看点:类型化.异步.集合. Hack最基础的特性就是类型标注.PHP5已经开始支持对象的类型化,PHP7也提供了标量类型化声明.Hack提供了全面的类型标注支持,与其typech ...

  3. Hack语言特性之类型化

    Hack最基础的特性就是类型标注.PHP5已经开始支持对象的类型化,PHP7也提供了标量类型化声明.Hack提供了全面的类型标注支持,与其typecher配合使用,还可以实现快速.前置静态类型验证. ...

  4. JVM中的动态语言支持简介

    抽丝剥茧 细说架构那些事——[优锐课] 从版本6开始,JVM已扩展为支持现代动态语言(也称为脚本语言).Java8的发行为这一领域提供了更多动力.感到这种支持的必要性是因为Java作为一种语言固有地是 ...

  5. Hack语言的类型系统

    基础类型 PHP中主要的基础类型可以在Hack中进行显式类型标注.包含: bool int float string array resource <?hh namespace Hack\Use ...

  6. C语言笔记——简介与编译过程初探

    序言 从今天起,详细说说C语言.这一年多,在大多数语言和技术之间转了一大圈,终于看清楚了事实,决心静下心来好好学学C语言.初学者会认为C语言是个入门用的东西,没有必要深入研究.但对计算机领域再稍加了解 ...

  7. Hack 语言学习/参考---1.3 Summary

    Summary Hack provides the following, non-exhaustive list of features: Ability to annotate function a ...

  8. Hack 语言学习/参考---1.2 Hack Background

    Hack Background Facebook was initially built with PHP. Parameters and return types were specified in ...

  9. Hack 语言学习/参考---1.1 What is Hack?

    What is Hack?¶ Hack is a language for HHVM that interopates seamlessly with PHP. The barrier to entr ...

随机推荐

  1. 看svn用户组管理功能的产品设计

    我负责公司的svn配置.用了近一年了,今天饶有兴致,分享一下svn的用户组管理功能,这个产品设计值得借鉴,简单易用. svn用户组管理的功能描述:新建用户组,并给组分配成员用户:编辑用户组,包括修改组 ...

  2. Atitit 代理CGLIB 动态代理 AspectJ静态代理区别

    Atitit 代理CGLIB 动态代理 AspectJ静态代理区别 1.1. AOP 代理主要分为静态代理和动态代理两大类,静态代理以 AspectJ 为代表:而动态代理则以 spring AOP 为 ...

  3. Atitit 通过调用gui接口杀掉360杀毒 360卫士  qq保镖等难以结束的进程(javac# php )

    Atitit 通过调用gui接口杀掉360杀毒 360卫士  qq保镖等难以结束的进程(javac# php ) 1.1. 这些流氓软件使用操作系统os提供的普通api根本就杀不掉啊1 1.2. 使用 ...

  4. CSS3常用属性

    CSS是我们常用的控制网页样式和布局的一种标准. CSS3是最新的CSS标准. CSS3被拆分为"模块",旧的规范也已经拆分为小的块,同时还增加了新的属性. 一些比较重要的CSS3 ...

  5. CI Weekly #2 | 如何优化开发流程,实现项目持续集成?

    原文首发于 flow.ci Blog >> 链接,转载请联系:) CI Weekly 围绕『 软件工程效率提升』 进行一系列技术内容分享,包括国内外持续集成.持续交付,持续部署.自动化测试 ...

  6. fir.im Weekly - 除了写代码,还需要了解什么

    雾霾天,宜撸代码.吹牛,不宜出门约会(¬_¬)ノ 本期 fir.im Weekly 亦如往期,收集了优秀的  iOS/Android 开发资源,GitHub 源码.前端方面的热点分享.除了代码,也许你 ...

  7. HTML中的title换行问题

    有时我们需要在html元素的title中换行显示信息.以下几种方法均可以实现: 直接在titile属性中使用换行,如下所示: asd">asdsadsad</a> 在tit ...

  8. linux安全检查

    1 ssh后门 检察语句: grep -E "user,pas|user:pas" /usr/bin/* /usr/local/sbin/* /usr/local/bin/* /b ...

  9. java中string内存的相关知识点

    (一):区别java内存中堆和栈: 1.栈:数据可以共享,存放基本数据类型和对象的引用,其中对象存放在堆中,对象的引用存放在栈中: 当在一段代码块定义一个变量时,就在栈中 为这个变量分配内存空间,当该 ...

  10. -bash: /usr/local/bin/react-native: No such file or directory

    执行react-native run-android/run-ios的时候出现 -bash: /usr/local/bin/react-native: No such file or director ...