hack類型常量:示例

2018-12-01 11:08 更新

下面是一些類型常量在嘗試決定是使用它們還是泛型類型參數(shù)化時可能有用的示例。

引用類型常量

引用類型常量與引用靜態(tài)類常量一樣簡單。

<?hh

namespace Hack\UserDocumentation\TypeConstants\Exampes\Examples\Referencing;

/*
 With generics, you reference the type parameter through explicit type
 parameterization declarations on all classes, functions, traits and interfaces
 using that type. In some cases that could be redundant because we know
 the type implicitly by knowing the type of the parent class.
 */
abstract class UserG<Tg as arraykey> {
  public function __construct(private Tg $id) {}
  public function getID(): Tg {
    return $this->id;
  }
}

class AppUserG<Tg as int> extends UserG<Tg> {}

// Still have to parameterize this function even though we know what Tg is.
function get_id_from_userg<Tg as arraykey>(AppUserG $ug): Tg {
  return $ug->getID();
}


/*
 With type constants, we can avoid having to declare the type parameter on a
 child class and function. The only thing needed in a child class is the
 concrete type binding on the abstract type constant of the parent. Otherwise,
 we just use reference the type constant as a normal property.
 */

abstract class UserTC {
  abstract const type Ttc as arraykey;
  public function __construct(private this::Ttc $id) {}
  public function getID(): this::Ttc {
    return $this->id;
  }
}

class AppUserTC extends UserTC {
  const type Ttc = int;
}

function get_id_from_userTC(AppUserTC $uc): AppUserTC::Ttc {
  return $uc->getID();
}

function run(): void {
  $aug = new AppUserG(-1);
  var_dump(get_id_from_userg($aug));
  $autc = new AppUserTC(-2);
  var_dump(get_id_from_userTC($autc));
}

run();

Output

int(-1)
int(-2)

覆蓋類型常量

對于在類中聲明的類型常量,可以提供約束和具體類型。當提供約束時,這允許類型常量被子類覆蓋。接口不支持此功能。

<?hh

namespace Hack\UserDocumentation\TypeConstants\Exampes\Examples\Overriding;

abstract class BaseAbstract {
  abstract const type T;
}

class ChildWithConstraint extends BaseAbstract {
  // We can override this constraint in a child of this concrete class
  // since we provided an explicit as constraint.
  const type T as ?arraykey = ?arraykey;
}

class ChildOfChildWithNoConstraint extends ChildWithConstraint {
  // Cannot override this in a child of this class.
  const type T = arraykey;
}

class ChildOfChildOfChildWithNoConstraint extends ChildOfChildWithNoConstraint {
  // Type error here
  const type T = string;
}

function run(): void {
  echo "No real output!";
}

run();

Output

No real output!

類型常量和實例方法

您可以使用類型常量作為類實例方法的輸入。

<?hh

namespace Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance;

abstract class Box {
  abstract const type T;
  public function __construct(private this::T $value) {}
  public function get(): this::T {
    return $this->value;
  }
  public function set(this::T $val): this {
    $this->value = $val;
    return $this;
  }
}

class IntBox extends Box {
  const type T = int;
}

function run(): void {
  $ibox = new IntBox(10);
  $ibox->set(11);
  var_dump($ibox);
  invariant($ibox instanceof Box, 'Upcast to Box');
  var_dump($ibox);
  // CHECK THIS -- THIS SHOULD (?) ERROR BUT THE TYPECHECKER IS NOT CATCHING IT
  // This will be an error because 'this::T' in '$box' may not be an int
  $ibox->set(1337);
  var_dump($ibox);
  // This is not an error because the type checker knows that the type
  // returned by $box->get(), is the same accepted by $box->set()
  $ibox->set($ibox->get());
  var_dump($ibox);
}

run();

Output

object(Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance\IntBox)#1 (1) {
  ["value":"Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance\Box":private]=>
  int(11)
}
object(Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance\IntBox)#1 (1) {
  ["value":"Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance\Box":private]=>
  int(11)
}
object(Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance\IntBox)#1 (1) {
  ["value":"Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance\Box":private]=>
  int(1337)
}
object(Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance\IntBox)#1 (1) {
  ["value":"Hack\UserDocumentation\TypeConstants\Exampes\Examples\Instance\Box":private]=>
  int(1337)
}
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號