
Description
Since I've been doing some work with the deriving code in libsyntax, I thought I'd solicit opinions on adding a new trait to libcore:
trait Enum {
static fn enumerate(blk: &fn(Self) -> bool);
}
Obviously, this would be most useful for enum
s with only nullary variants, but other enumerable types could take advantage of it as well (bool::all_values
currently serves the same purpose).
This would make it possible to write
// enum Dir { North, East, South, West }
for Enum::enumerate |dir: Dir| { ... }
instead of
for [North, East, South, West].each |dir| { ... }
A standard implementation for enum
s with only nullary variants would be made available through #[deriving(Enum)]
, and a default method (or utility trait/impl until those are working) would be provided for obtaining all the values in a vector. It might be beneficial to have a static fn cardinality() -> Option<uint>
method on the trait as well.
If the trait name is too easily confused with the keyword, another option is Enumerable
.