2
在與黑客的通用功能的限制試驗,我編造了以下內容:Hacklang - 是否可以使用類型常量的類型約束?
<?hh // strict
class Base {}
class Derived extends Base {}
abstract class ImplBase {
abstract const type T as Base;
public function foo<Tf as this::T>(Tf $v): void {}
}
class ImplDerived extends ImplBase {
const type T = Derived;
}
function foo(ImplDerived $v): void {
bar($v);
}
function bar(ImplBase $v): void {
$v->foo(new Base());
$v->foo(new Derived());
}
原理是基本夠用:向上轉型從具體ImplDerived
,請問typechecker治療ImplBase::T
爲一個抽象類型bar
能按約束投射到Base
,還是保留對T = Derived
的引用?奇怪的是,答案既不是。在bar
每行一個不同的原因而失敗:
$v->foo(new Base());
因爲Tf
是「約束爲一個依賴型<expr#1>::T
[...]參照new Base()
」失敗。$v->foo(new Derived());
失敗,因爲poof!Tf
突然變成了Base
! 「[this::T]
是類型Base
的一個對象,由於擴展類型常量ImplBase::T
而產生」。
我問不可能,或者這可能還沒有完全實現嗎?