codecamp

Typechecker简介

Hack typechecker是使Hack成为一种独特语言的主要工具。在运行时间之前,typechecker会分析与程序相关的所有与各种打字错误相关的代码,从而防止可能只在运行时暴露的恶作剧。

没有typechecker,这个简单的例子将在运行时失败。

<?php

namespace Hack\UserDocumentation\TypeChecker\Intro\Examples\RuntimeFail;

class A {
  public function foo() { return 2; }
}

function failing($x) {
  $a = new A();
  if ($x === 4) {
    $a = null;
  }
  // $a being null would only be caught at runtime
  // Fatal error: Uncaught exception 'BadMethodCallException' with message
  // 'Call to a member function foo() on a non-object (NULL)'
  $a->foo();
}

failing(4);

Output

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function foo() on a non-object (null)' in /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/runtime-fail.php:17
Stack trace:
#0 /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/runtime-fail.php(20): Hack\UserDocumentation\TypeChecker\Intro\Examples\RuntimeFail\failing()
#1 {main}

但是,在运行代码之前使用typechecker,您可以在部署之前捕获错误,从而将用户从可能发生的恶意致命中保存。

<?hh

namespace Hack\UserDocumentation\TypeChecker\Intro\Examples\TypecheckerCatch;

class A {
  public function foo() { return 2; }
}

function failing($x) {
  $a = new A();
  if ($x === 4) {
    $a = null;
  }
  // $a being null would only be caught BEFORE runtime
  //   typechecker-catch.php:21:7,9: You are trying to access the member foo
  //                                 but this object can be null. (Typing[4064])
  //   typechecker-catch.php:12:10,13: This is what makes me believe it can be
  //                                   null
  //
  $a->foo();
}

failing(4);

Output

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function foo() on a non-object (null)' in /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/typechecker-catch.php.type-errors:20
Stack trace:
#0 /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/typechecker-catch.php.type-errors(23): Hack\UserDocumentation\TypeChecker\Intro\Examples\TypecheckerCatch\failing()
#1 {main}

typechecker静态分析您的程序,捕捉代码所有路径中的问题。这不是汇编。它是对程序中可能出现的各种状态的超快速反馈,因此您可以在代码运行之前处理它们。Typechecker可以实时监控代码的更改并相应地更新其分析。

结合类型注释的功能,Typechecker是一个强大的错误捕获工具。


XHP:指南
Typechecker安装
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }