2

再会,

我有问题。我想模拟hacklang中的一些错误。

<?hh
namespace Exsys\HHVM;

class HHVMFacade{

    private $vector = Vector {1,2,3};

    public function echoProduct() : Vector<string>{
        return $this->vector;
    }

    public function test(Vector<string> $vector) : void{
        var_dump($vector);
    }

}

函数 echoProduct() 返回字符串向量。但是私有属性 $vector 是整数向量。当我调用 echoFunction 并返回值用作函数 test() 的参数时。我明白了

object(HH\Vector)#35357 (3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

为什么?我期待一些错误,因为类型不匹配。

4

1 回答 1

8

这里有两件事在起作用:

  1. 泛型没有具体化,因此运行时没有关于它们的信息。这意味着运行时仅检查您是否返回了Vector.
  2. $this->vector本身没有输入。这意味着类型检查器 ( hh_client) 将其视为未知类型。Vector<string>未知类型与所有内容匹配,因此在预期a 的地方返回未知类型没有问题。

    这是为了让您逐渐键入您的代码。每当一个类型未知时,类型检查器只是假设开发人员知道发生了什么。

我要做的第一件事是将文件从部分模式更改为严格模式,这仅涉及从更改<?hh<?hh // strict. 这会导致类型检查器抱怨任何丢失的类型信息(以及其他一些事情,比如没有超全局变量,你不能调用非 Hack 代码)。

这会产生错误:

test.hh:6:13,19: Please add a type hint (Naming[2001])

如果您随后键入$vector( Vector<int>) private Vector<int> $vectorhh_client则生成:

test.hh:9:16,28: Invalid return type (Typing[4110])
  test.hh:8:44,49: This is a string
  test.hh:6:20,22: It is incompatible with an int
  test.hh:8:44,49: Considering that this type argument is invariant with respect to Vector

这是您预期的错误。你也可以通过简单地添加类型来得到这个错误$vector,而不用切换到严格模式,尽管我更喜欢在代码支持的最强模式下编写我的 Hack。

对于较新版本的 HHVM,每当运行 Hack 代码时都会调用类型检查器(有一个 INI 标志可以将其关闭),因此导致类型不匹配也会导致代码执行失败。

于 2015-10-08T21:48:49.067 回答