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

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

<?php

namespace Hack\UserDocumentation\Types\Intro\Examples\PreHack;

class Z {}
class A {
public $a;
public $b; public function __construct($a, $b) {
$this->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();
Output
int(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()函数来检测或抛出异常可以缓解这种情况。

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

<?hh

namespace Hack\UserDocumentation\Types\Intro\Examples\Hack;

class Z {}
class A {
public int $a;
public int $b; public function __construct(int $a, int $b) {
$this->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 string
hack.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 bool
hack.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 bool
hack.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
*****/
Output
int(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,你就有了一款真正的很强的安全基础的动态编程语言。

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

  • 类型系统
  • 类型标注

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. jetty

    相关的文章太多了,我只按照自己的意思做简单总结. 参见: http://www.cnblogs.com/duanxz/p/3154982.html http://www.cnblogs.com/win ...

  2. DDD~领域层

    回到目录 再论Domain与Infrastructure 在面向领域的设计中,领域层(Domain)实现上是位于最底层的,其它层有对它的引用,包括基础设施层(Infrastructure)也是去引用领 ...

  3. Atitit 图像清晰度 模糊度 检测 识别 评价算法 原理

    Atitit 图像清晰度 模糊度 检测 识别 评价算法 原理 1.1. 图像边缘一般都是通过对图像进行梯度运算来实现的1 1.2. Remark: 1 1.3.  1.失焦检测. 衡量画面模糊的主要方 ...

  4. fir.im Weekly - 从零开始创建 Android 新项目

    今年的 Google I/O 大会上,人工智能和虚拟现实的产品发布让我们对未来多了几分惊喜.对于开发者部分,Google 发布了 Android N 系统,感受最深的是全新的 Android Stud ...

  5. Searching for a valid kernel header path... The path "" is not a valid path to the ***** kernel headers. Would you like to change it? [yes]

    在centos 6.5安装vmtools时候,解压包,mount挂载后,安装pl过程中报 这个问题,半天没有解决,google 后发现这样就行了 I installed the kernel-deve ...

  6. jquery的html,text,val

    .html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对比 .html(),.text() ...

  7. angularjs的resource实例对象

    angularjs的resource实例对象 我们看看都有啥 而直接使用service对象的时候没有前面这些$

  8. 通过bootstrap来学习less

    很早之前就听说过less了,但是一直拖着没去学习.最近抽空看了less,其实语法很简单,看一遍基本就知道怎么用了.平时自己写页面用less的话,感觉是方便了些,但是难道less的好处就只是这样? 刚好 ...

  9. 深入理解CSS线性渐变linear-gradient

    × 目录 [1]定义 [2]渐变线 [3]色标 [4]重复渐变 [5]多背景 [6]应用场景 [7]IE兼容 前面的话 在CSS3出现之前,渐变效果只能通过图形软件设计图片来实现,可拓展性差,还影响性 ...

  10. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)一

    MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中. 使用这个类库中的类, Spring 将会加载必要的MyBatis工厂类和 session 类. 这个类库 ...