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 ...
随机推荐
- CSS的两个小知识点 伪类选择器和display:table-cell
li:first-child {color:red} li:nth-child(3n) {color: red} 在对nth-child传参的时候,已经直接用公式,3n就表示3的倍数.多用伪类和伪元素 ...
- 将不确定变为确定~transactionscope何时提升为分布式事务~SQL2005与SQL2008不同
回到目录 Transactionscope何时被提升为分布式事务,即时要触发msdtc服务,这个问题与数据库版本有关,在前面的文章中,我的MSTDC系列出现了多个版本,有一点没有说清楚,测试的环境不同 ...
- 搭建jekyll博客
使用jekyll将markdown文件生成静态的html文件,并使用主题有序的进行布局,形成最终的博客页面. 特点 基于ruby 使用Markdown书写文章 无需数据库 可以使用GitHub Pag ...
- Android 在View中更新View
直接用Invalidate()方法会导致错误:只有主线程才能更新UI 取而代之的是可以使用postInvalidate(); 原因: 最终会调用ViewRootImpl类的dispatchInvali ...
- golang开发缓存组件
代码地址github:cache 花了一天时间看了下实验楼的cache组件,使用golang编写的,收获还是蛮多的,缓存组件的设计其实挺简单的,主要思路或者设计点如下: 全局struct对象:用来做缓 ...
- Servlet开发技术,创建,以及Servlet的配置,web.xml的配置
直接上图,不废话!!! 第一:首先在Eclipse的包资源管理器中,单机鼠标右键,在弹出的快捷键菜单中选择“新建”/Servlet命令,在弹出的对话框中输入新建的Servlet所在的包和类名,然后单击 ...
- javascript中function 函数递归的陷阱问题
//看下这个递归方法,最后输出的值function fn(i){ i++; if(i<10){ fn(i); } else{ return i; } } var result = fn(0); ...
- Oracle 11g系列:数据库
1.创建Oracle数据库 创建Oracle数据库的最常用工具为Database Configuration Assistant(数据库配置助手),依次选择[开始]|[所有程序]|[Oracle-Or ...
- 深入理解PHP内核(十二)函数-函数的定义、传参及返回值
原文链接:http://www.orlion.ga/344/ 一.函数的定义 用户函数的定义从function 关键字开始,如下 function foo($var) { echo $var; ...
- AIX下如何根据端口号查找相应的进程
1. $ netstat -Aan |grep 8080 f1000e0002321bb8 tcp 0 0 *.8080 *.* LISTEN 2. $ rmsock f1000e0002321bb8 ...