W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
考慮具有公共初始字段序列的兩種形狀類型。例如:
enum Bank: int {
DEPOSIT = 1;
// ...
}
type Transaction = shape('trtype' => Bank);
type Deposit = shape('trtype' => Bank, 'toaccnum' => int, 'amount' => float);
具有較大字段集的形狀類型Deposit
是具有較小字段集的子類型Transaction
。前者有后者的所有領域,所以前者的價值可以用來代替后者。例如,您現(xiàn)在可以編寫一個對所有具有稱為“ '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:
(例如)來確定事務的實際類型也是如此。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: