99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

hack類(lèi)型常量:Constraints

2018-11-30 11:38 更新

類(lèi)型常量的約束(Constraints)類(lèi)似于泛型類(lèi)型參數(shù)的約束,因?yàn)樗鼈兿拗祁?lèi)型常量可能被覆蓋的內(nèi)容。

抽象類(lèi)

對(duì)抽象類(lèi)型常量的約束要求任何覆蓋抽象類(lèi)型常量的類(lèi)必須是約束類(lèi)型的子類(lèi)型。

<?hh

namespace Hack\UserDocumentation\TypeConstants\Constraints\Examples\AbsClass;

abstract class AbsParent {
  abstract const type Foo as arraykey;
}

class FirstChild extends AbsParent {
  const type Foo = string; // good since string is arraykey.
}

class SecondChild extends AbsParent {
  const type Foo = float; // typechecker error since float is not an arraykey.
}

/***

abstract-type-errors.php:13:7,17: Class Hack\UserDocumentation\TypeConstants\Constraints\Examples\AbsClass\SecondChild does not correctly implement all required methods  (Typing[4110])
  abstract-type-errors.php:13:27,35: Some methods are incompatible with those declared in type Hack\UserDocumentation\TypeConstants\Constraints\Examples\AbsClass\AbsParent
Read the following to see why:
  abstract-type-errors.php:6:23,25: Unable to satisfy constraint on this type constant
  abstract-type-errors.php:6:30,37: This is an array key (int/string)
  abstract-type-errors.php:14:20,24: It is incompatible with a float

***/

Concrete Classes

一個(gè)具體的類(lèi)型常量必須有一個(gè)約束,以便任何孩子覆蓋該類(lèi)型的常量。

<?hh

namespace Hack\UserDocumentation\TypeConstants\Constraints\Examples\ConcClass;

class ParentWithConstraint {
  const type Foo as arraykey = arraykey;
}

class Child extends ParentWithConstraint {
  // good since string is arraykey and parent constrained to arraykey.
  const type Foo = string;
}

class ParentWithoutConstraint {
  const type Foo = arraykey;
}

class BadChild extends ParentWithoutConstraint {
  // Although int is an arraykey, parent not constrained to arraykey, so this
  // is a typechecker error.
  const type Foo = int;
}


/***

concrete-type-errors.php:18:7,14: Class Hack\UserDocumentation\TypeConstants\Constraints\Examples\ConcClass\BadChild does not correctly implement all required methods  (Typing[4110])
  concrete-type-errors.php:18:24,46: Some methods are incompatible with those declared in type Hack\UserDocumentation\TypeConstants\Constraints\Examples\ConcClass\ParentWithoutConstraint
Read the following to see why:
  concrete-type-errors.php:21:14,16: The assigned type of this type constant is inconsistent with its parent
  concrete-type-errors.php:21:20,22: This is an int
  concrete-type-errors.php:15:20,27: It is incompatible with an array key (int/string)

***/
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)