05 June 2005
This post may be outdated due to it was written on 2005. The links may be broken. The code may be not working anymore. Leave comments if needed.

Enum, enumerate 的缩写,可翻译为枚举。
在 Larry Wall's A12 中,Enum 用于简化类似下面这样的声明:

my subtype DayOfWeek of Int where 0..6;
my subtype DayOfWeek::Sunday    of DayOfWeek where 0;
my subtype DayOfWeek::Monday    of DayOfWeek where 1;
my subtype DayOfWeek::Tuesday   of DayOfWeek where 2;
my subtype DayOfWeek::Wednesday of DayOfWeek where 3;
my subtype DayOfWeek::Thursday  of DayOfWeek where 4;
my subtype DayOfWeek::Friday    of DayOfWeek where 5;
my subtype DayOfWeek::Saturday  of DayOfWeek where 6;
这是一种很常见的结构。一个类只允许几个已知的值,这就是枚举的大概意思。上面的代码可以简化为:
my enum DayOfWeek of int
        «Sunday Monday Tuesday Wednesday Thursday Friday Saturday»;
如果你想把 Sunday 为 7,且采用缩写的话,可以这么做:
my enum DayOfWeek <<:Mon(1) Tue Wed Thu Fri Sat Sun>>;

枚举这种结构是很常见的,比如罗马数字或十六进制等:

my enum roman [i => 1,   v => 5,
                x => 10,  l => 50,
                c => 100, d => 500,
                m => 1000];
my Scalar hex «:zero(0) one two three four five six seven eight nine
                   :ten eleven twelve thirteen fourteen fifteen»;

Usage

enum 可以表现为一个 role 或 property, 而 enum 的值可以表现为一个子类型,方法或者一个普通值(不带参数的 sub)。
我们以 enum day <Sun Mon Tue Wed Thu Fri Sat> 为例:
my $x does Tue; # Tue 做为 role
$x does day<Tue>; # 上一句其实等同于这一句
say $x.day; # 3, enum 定义了 day::Tue 为 3
$x ~~ day; $x ~~ Tue; $x ~~ day::Tue; # 都返回 ture
$x ~~ Wed; # 返回 false
前面的
role Example13 { # Perl6 } 里补充的 but 是 enum 最好的使用方法。
$x = "Today" but Tue; # 等同与 $x = "Today" but day::Tue; 或 $x = "Today" but day<Tue>;
用前面 but 的解释就是:
$x = "Today";
$x does day;
$x.day = &day::('Tue');
下面这些都将返回 true
$x.does(Tue)
$x.does(day)
$x.day == Tue
day($x) == Tue
Tue($x)
$x.Tue

我们有两个内建的 enum:

our bit enum *bool <false true>;
our bit enum *taint <untainted tainted>;

Others

关于冲突与覆盖,请参考 Perl6::Bible::A12Perl6::Bile::S12.


blog comments powered by Disqus