哈克语言类型化简介

要想领悟代码中能够运用什么项目以及在哪纺织显式类型标注,查看:

  • 种类系统
  • 品类标注

在typechecker的十三分下,哈克语言的类型化能力是哈克其余功能特色的水源。开发哈克语言的根本思想也便是为代码提供显式类型标注以便对代码进行项目一致性和秘密错误分析。
那是用于比较哈克个性的1个实例,用守旧PHP格局编写:

不过你看看上面包车型客车例证,开发者的来意是或不是更清晰:

<?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

上述示范能够圆满运维于HHVM上(除了在最后的var_dump处发生3个沉重错误)。固然如此,在不少地点它并未清晰的表述出开发者的意向。比如,开发者是不是允许A::foo()接受字符串类型的int值。当然,习惯上的处理方式,如通过is_int()函数来检查和测试或抛出非凡能够消除那种情况。

见到那个例子里的代码,大家得以很清楚的了然仅允许传递int型值。那样API的调用者知道需求传递什么品种的值。为艺术和总体性添加显式类型注脚,合作哈克typechecker,你就有了一款真正的很强的含笑花基础的动态编制程序语言。

<?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

相关文章