codecamp

hack形状子类型化

考虑具有公共初始字段序列的两种形状类型。例如:

enum Bank: int {
  DEPOSIT = 1;
  // ...
}

type Transaction = shape('trtype' => Bank);
type Deposit = shape('trtype' => Bank, 'toaccnum' => int, 'amount' => float);

具有较大字段集的形状类型Deposit是具有较小字段集的子类型Transaction。前者有后者的所有领域,所以前者的价值可以用来代替后者。例如,您现在可以编写一个对所有具有称为“ 'trtype'类型Bank” 字段的形状进行操作的函数。例如:

<?hh

namespace Hack\UserDocumentation\Shapes\Examples\Subtyping\Subtype;

enum Bank: int {
  INVALID = 0;
  DEPOSIT = 1;
  WITHDRAWAL = 2;
  TRANSFER = 3;
}

type Transaction = shape('trtype' => Bank);
type Deposit = shape('trtype' => Bank, 'toaccnum' => int, 'amount' => float);
type Withdrawal = shape('trtype' => Bank, 'fromaccnum' => int,
                        'amount' => float);
type Transfer = shape('trtype' => Bank, 'fromaccnum' => int,
                      'toaccnum' => int, 'amount' => float);

function processTransaction(Transaction $t): void {
  var_dump($t);

  $a = Shapes::toArray($t);
  var_dump(count($a), $a);

  $v = Shapes::idx($t, 'trtype', Bank::INVALID);  // checker accepts this
  var_dump($v);

  $v = Shapes::keyExists($t, 'trtype');   // checker accepts this
  var_dump($v);

  Shapes::removeKey($t, 'xyz');   // checker accepts this
  var_dump($t);

  // checker complains Invalid argument (Typing[4140])
  $v = Shapes::idx($t, 'amount', -999.0); // The field 'amount' is missing
  var_dump($v);

  // checker complains Invalid argument (Typing[4140])
  $v = Shapes::keyExists($t, 'amount'); // The field 'amount' is missing
  var_dump($v);

  // checker is fine here because we used removeKey above
  $v = Shapes::keyExists($t, 'xyz'); // The field 'xyz' is missing
  var_dump($v);

  switch ($t['trtype']) {
    case Bank::TRANSFER:
      echo "Transfer: " . $t['amount'] . " from Account " . $t['fromaccnum'] .
           " to Account " . $t['toaccnum'] . "\n";
      break;
    case Bank::DEPOSIT:
      // The field amount is undefined (Typing[4108])
      // The field toaccnum is undefined (Typing[4108])
      echo "Deposit: " . $t['amount'] . " to Account " . $t['toaccnum'] . "\n";
      break;
    case Bank::WITHDRAWAL:
      echo "Withdrawal: " . $t['amount'] . " from Account " .
           $t['fromaccnum'] . "\n";
      break;
    default:
      break;
  }
}

function main(): void {
  processTransaction(shape('trtype' => Bank::DEPOSIT, 'toaccnum' => 23456,
                           'amount' => 100.00));
  processTransaction(shape('trtype' => Bank::WITHDRAWAL, 'fromaccnum' => 3157,
                           'amount' => 100.00));
  processTransaction(shape('trtype' => Bank::TRANSFER, 'fromaccnum' => 23456,
                           'toaccnum' => 3157, 'amount' => 100.00));
}

main();

Output

array(3) {
  ["trtype"]=>
  int(1)
  ["toaccnum"]=>
  int(23456)
  ["amount"]=>
  float(100)
}
int(3)
array(3) {
  ["trtype"]=>
  int(1)
  ["toaccnum"]=>
  int(23456)
  ["amount"]=>
  float(100)
}
int(1)
bool(true)
array(3) {
  ["trtype"]=>
  int(1)
  ["toaccnum"]=>
  int(23456)
  ["amount"]=>
  float(100)
}
float(100)
bool(true)
bool(false)
Deposit: 100 to Account 23456
array(3) {
  ["trtype"]=>
  int(2)
  ["fromaccnum"]=>
  int(3157)
  ["amount"]=>
  float(100)
}
int(3)
array(3) {
  ["trtype"]=>
  int(2)
  ["fromaccnum"]=>
  int(3157)
  ["amount"]=>
  float(100)
}
int(2)
bool(true)
array(3) {
  ["trtype"]=>
  int(2)
  ["fromaccnum"]=>
  int(3157)
  ["amount"]=>
  float(100)
}
float(100)
bool(true)
bool(false)
Withdrawal: 100 from Account 3157
array(4) {
  ["trtype"]=>
  int(3)
  ["fromaccnum"]=>
  int(23456)
  ["toaccnum"]=>
  int(3157)
  ["amount"]=>
  float(100)
}
int(4)
array(4) {
  ["trtype"]=>
  int(3)
  ["fromaccnum"]=>
  int(23456)
  ["toaccnum"]=>
  int(3157)
  ["amount"]=>
  float(100)
}
int(3)
bool(true)
array(4) {
  ["trtype"]=>
  int(3)
  ["fromaccnum"]=>
  int(23456)
  ["toaccnum"]=>
  int(3157)
  ["amount"]=>
  float(100)
}
float(100)
bool(true)
bool(false)
Transfer: 100 from Account 23456 to Account 3157

但是有一个重要的警告。内部功能processTransaction唯一的领域是你可以访问$t'trtype'。即使您使用交换机case Bank::DEPOSIT:(例如)来确定事务的实际类型也是如此。

hack形状功能
hack形状案例
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }