Hack语言类型化简介
在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语言类型化简介的更多相关文章
- Facebook Hack 语言 简介
1. Hack 是什么? Hack 是一种基于HHVM(HipHop VM 是Facebook推出的用来执行PHP代码的虚拟机,它是一个PHP的JIT编译器,同时具有产生快速代码和即时编译的优点.)的 ...
- Facebook的Hack语言三大看点
Hack语言主要有三大看点:类型化.异步.集合. Hack最基础的特性就是类型标注.PHP5已经开始支持对象的类型化,PHP7也提供了标量类型化声明.Hack提供了全面的类型标注支持,与其typech ...
- Hack语言特性之类型化
Hack最基础的特性就是类型标注.PHP5已经开始支持对象的类型化,PHP7也提供了标量类型化声明.Hack提供了全面的类型标注支持,与其typecher配合使用,还可以实现快速.前置静态类型验证. ...
- JVM中的动态语言支持简介
抽丝剥茧 细说架构那些事——[优锐课] 从版本6开始,JVM已扩展为支持现代动态语言(也称为脚本语言).Java8的发行为这一领域提供了更多动力.感到这种支持的必要性是因为Java作为一种语言固有地是 ...
- Hack语言的类型系统
基础类型 PHP中主要的基础类型可以在Hack中进行显式类型标注.包含: bool int float string array resource <?hh namespace Hack\Use ...
- C语言笔记——简介与编译过程初探
序言 从今天起,详细说说C语言.这一年多,在大多数语言和技术之间转了一大圈,终于看清楚了事实,决心静下心来好好学学C语言.初学者会认为C语言是个入门用的东西,没有必要深入研究.但对计算机领域再稍加了解 ...
- Hack 语言学习/参考---1.3 Summary
Summary Hack provides the following, non-exhaustive list of features: Ability to annotate function a ...
- Hack 语言学习/参考---1.2 Hack Background
Hack Background Facebook was initially built with PHP. Parameters and return types were specified in ...
- Hack 语言学习/参考---1.1 What is Hack?
What is Hack?¶ Hack is a language for HHVM that interopates seamlessly with PHP. The barrier to entr ...
随机推荐
- MyBatis学习总结(七)——Mybatis缓存
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- 【管理心得之三十八】如果“Q”不是高富帅,也吸引不了白富美“A”
场景再现=========================={美剧片段}一位老人在电话亭中,一次又一次地向公用电话投硬币,但是每一次仅是接通后就自动掉线了.老人无奈之下寻求他人拨打报修电话,但苦等了许 ...
- 关于 fir.im 你可能不知道的实用小工具
大家可能都知道 fir.im 是做测试发包的,上传你的 IPA/APK, 测试用户可以通过一个短链接和二维码就可快速安装测试. 除了基本的发包功能即应用上传下载外,fir.im 还为提高发包体验提供了 ...
- 转载:Spring AOP (上)
工 作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大说过:“开发人员不可能一天到晚只有工作,肯定是需要自我学习.第一:为了更充实自己,保 持进步状态.第二:为了提升技术,提高开发能力.第三 ...
- 类的继承和多态性-编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,
编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 ...
- 咱们来聊聊JS中的异步,以及如何异步,菜鸟版
为什么需要异步?why?来看一段代码. 问题1: for(var i=0;i<100000;i++){ } alert('hello world!!!'); 这段代码的意思是执行100...次后 ...
- 自引用指针this
C++为成员函数提供了一个名字为this的指针,这个指针称为自引用指针,每当创建一个对象时,系统就把this指针初始化为指向该对象,即this指针的值是当前调用成员函数的对象的起始地址.每当调用一个成 ...
- c++实现kd树
#ifndef _KD_TREE_H_ #define _KD_TREE_H_ #include <memory> #include <vector> #include < ...
- ProgressBar 源码
/** * @FileName CircleProgressBar.java * @Package com.read.view * @Description TODO * @Author Alpha ...
- 深入理解CSS计数器
× 目录 [1]创建计数器 [2]使用计数器 [3]DEMO 前面的话 我们对计数器已经不陌生了,有序列表中的列表项标志就是计数器. 创建计数器 创建计数器的基础包括两个方面,一是能重置计数器的起点, ...