W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
如果您熟悉C#,C ++或Java等語言中的枚舉(enumerations),那么Hack枚舉將讓您感到賓至如歸。枚舉封裝了一組相關(guān)的常量,與使用全局常量或類常量不同。枚舉實(shí)際上創(chuàng)建了一個新的類型,可以通過名稱進(jìn)行注釋。
注意:在這一點(diǎn)上,hack只支持int和string枚舉。
枚舉的語法相對簡單,與其他支持枚舉的語言類似。
enum <NameOfEnum>: <string | int> {
<Member1> = <value>;
<Member2> = <value>;
:
:
<MemberN> = <value>;
}
枚舉的名稱必須是名稱空間唯一的,并且枚舉的成員也必須唯一命名。
<?hh
namespace Hack\UserDocumentation\Enums\Intro\Examples\Simple;
enum Size: int {
SMALL = 0;
MEDIUM = 1;
LARGE = 2;
X_LARGE = 3;
}
Output
枚舉成員的值必須與枚舉的類型相匹配。如果你的枚舉被標(biāo)記為一個int枚舉,那么成員值必須是int。而且,這些值必須能夠被靜態(tài)評估。換句話說,沒有變量等可以用作枚舉成員值的一部分。
要訪問成員值,可以使用類常量類似的語法
<NameOfEnum> :: <MemberN>
<?hh
namespace Hack\UserDocumentation\Enums\Intro\Examples\MemberValues;
enum Size: string {
SMALL = "small" ;
MEDIUM = "medium";
LARGE = "large";
X_LARGE = "x-large";
}
function say_all_sizes(): void {
echo Size::SMALL . PHP_EOL . Size::MEDIUM . PHP_EOL .
Size::LARGE . PHP_EOL . Size::X_LARGE;
}
say_all_sizes();
Output
small
medium
large
x-large
注意:枚舉成員沒有順序的隱式值的概念。例如,如果將第一個成員設(shè)置為0
,則默認(rèn)情況下,下一個成員的值為1
。每個枚舉成員必須有一個明確的值分配給它。
枚舉有底層的類型,但它們是不可互換的。枚舉是一個獨(dú)特的類型。和其他任何類型一樣,例如,你不能int把一個函數(shù)傳遞給一個枚舉,或者一個枚舉值給一個函數(shù)string。
但是,您可以使用簡單的轉(zhuǎn)換來將枚舉的成員值轉(zhuǎn)換為其基礎(chǔ)類型,并且可以使用Hack的特殊枚舉函數(shù) assert()并coerce()從基礎(chǔ)類型轉(zhuǎn)換為枚舉類型。
注:您可以使用一些語言結(jié)構(gòu),echo而不需要轉(zhuǎn)換為基礎(chǔ)類型。
<?hh
namespace Hack\UserDocumentation\Enums\Intro\Examples\Casting;
enum Size: string {
SMALL = "small" ;
MEDIUM = "medium";
LARGE = "large";
X_LARGE = "x-large";
}
function say_greeting_with_size(string $size): void {
echo "Hello. We have size " . $size . PHP_EOL;
}
function give_shirt(Size $size): void {
echo "Here is your shirt of size " . $size . PHP_EOL;
}
function sales(): void {
// We need a cast here because say_greeting_with_size is expecting a string
// and it is incompatible with the value of an enum (even though it is a
// string underlying).
say_greeting_with_size((string) Size::SMALL);
// To go from the underlying type to the enum type, Hack provides builtin
// enum functions to help you. The assert function basically tells the
// typechecker that you are NOT going to give it a value that doesn't exist
// in the enum.
give_shirt(Size::assert("small"));
}
sales();
Output
Hello. We have size small
Here is your shirt of size small
通過使用as約束運(yùn)算符,可以使其轉(zhuǎn)換為隱式的基礎(chǔ)類型。
<?hh
namespace Hack\UserDocumentation\Enums\Intro\Examples\ImplicitCasting;
// We are now telling the typechecker that this enum is constrained to the
// underlying string type, and that implicit casts should be allowed.
enum Size : string as string {
SMALL = "small" ;
MEDIUM = "medium";
LARGE = "large";
X_LARGE = "x-large";
}
function say_greeting_with_size(string $size): void {
echo "Hello. We have size " . $size . PHP_EOL;
}
function give_shirt(Size $size): void {
echo "Here is your shirt of size " . $size . PHP_EOL;
}
function sales(): void {
// Implict casting at work
say_greeting_with_size(Size::SMALL);
// No implicit casting this way though.
// To go from the underlying type to the enum type, Hack provides builtin
// enum functions to help you. The assert function basically tells the
// typechecker that you are NOT going to give it a value that doesn't exist
// in the enum.
give_shirt(Size::assert("small"));
}
sales();
Output
Hello. We have size small
Here is your shirt of size small
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: