hack枚举介绍
如果您熟悉C#,C ++或Java等语言中的枚举(enumerations),那么Hack枚举将让您感到宾至如归。枚举封装了一组相关的常量,与使用全局常量或类常量不同。枚举实际上创建了一个新的类型,可以通过名称进行注释。
注意:在这一点上,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
枚举成员的值
枚举成员的值必须与枚举的类型相匹配。如果你的枚举被标记为一个int枚举,那么成员值必须是int。而且,这些值必须能够被静态评估。换句话说,没有变量等可以用作枚举成员值的一部分。
要访问成员值,可以使用类常量类似的语法
<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
注意:枚举成员没有顺序的隐式值的概念。例如,如果将第一个成员设置为0
,则默认情况下,下一个成员的值为1
。每个枚举成员必须有一个明确的值分配给它。
铸造到底层类型
枚举有底层的类型,但它们是不可互换的。枚举是一个独特的类型。和其他任何类型一样,例如,你不能int把一个函数传递给一个枚举,或者一个枚举值给一个函数string。
但是,您可以使用简单的转换来将枚举的成员值转换为其基础类型,并且可以使用Hack的特殊枚举函数 assert()并coerce()从基础类型转换为枚举类型。
注:您可以使用一些语言结构,echo而不需要转换为基础类型。
<?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约束运算符,可以使其转换为隐式的基础类型。
<?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