Skip to content

RFC: Enum traitΒ #284

Open
Open
@rust-highfive

Description

@rust-highfive

Issue by ghost
Sunday Mar 17, 2013 at 02:56 GMT

For earlier discussion, see rust-lang/rust#5417

This issue was labelled with: A-libs, A-traits, B-RFC in the Rust repository


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 enums 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 enums 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-enumEnum related proposals & ideasA-traits-libstdStandard library trait related proposals & ideasT-langRelevant to the language team, which will review and decide on the RFC.T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions