-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Enum trait #5417
Comments
Maybe it makes more sense to have a method on the trait that returns an iterator instead, to better support lazy operations and the usual extended iterator methods. |
Haskell's Enum is similar but not quite: http://zvon.org/other/haskell/Outputprelude/Enum_c.html |
I would prefer something more like Haskell's Enum type class, with fromEnum and toEnum methods. That seems more general to me -- I think you could implement what you're proposing in terms of it. |
I think converting between enums and integers is a separate issue. This is just for those scenarios in which you need to access every variant of a C-like enum (or some other completely enumerable series, be it bitstrings, Fibonacci numbers, or the legal values of I'm proposing (in the trait Enumerable<I: BaseIter<Self>> {
fn enumerator() -> I;
} which would be implemented like enum Dir { North, East, South, West }
/* everything below this would be generated by `#[deriving(Enumerable)]` on the enum */
impl Enumerable<DirEnumerator> for Dir {
fn enumerator() -> DirEnumerator { DirEnumerator }
}
struct DirEnumerator;
impl BaseIter<Dir> for DirEnumerator {
fn each(&self, blk: &fn(&Dir) -> bool) {
[North, East, South, West].each(blk);
}
fn size_hint(&self) -> Option<uint> { Some(4) }
} Then you might have some theoretical trait for C-like enums like
|
@catamorphism thoughts on the clarification @apasel422 has provided? I certainly think what he has proposed would be incredibly useful (I've need that sort of thing before). Although, perhaps converting to/from integers could be provided on the same trait? |
@z0w0 I don't have a strong opinion. In any case, I don't think it should be on the backwards-compatible milestone. Nominating for feature-complete so we can discuss. |
@thestinger @huonw could this automatically be derived? I envision it just expanding to |
I think this is just a deriving mode that produces an iterator, yeah. Not bad, but it should just be a feature (and I'm not even sure it's one that ought to block feature-complete). It's neat but I could live without it. |
just a bug, removing milestone/nomination. |
triage: no change. I think calling it |
This issue has been moved to the RFCs repo: rust-lang/rfcs#284 |
Update doc links and mentioned names in docs changelog: none
Rollup of 11 pull requests Successful merges: - rust-lang#5406 (Fix update_lints) - rust-lang#5409 (Downgrade let_unit_value to pedantic) - rust-lang#5410 (Downgrade trivially_copy_pass_by_ref to pedantic) - rust-lang#5412 (Downgrade inefficient_to_string to pedantic) - rust-lang#5415 (Add new lint for `Result<T, E>.map_or(None, Some(T))`) - rust-lang#5417 (Update doc links and mentioned names in docs) - rust-lang#5419 (Downgrade unreadable_literal to pedantic) - rust-lang#5420 (Downgrade new_ret_no_self to pedantic) - rust-lang#5422 (CONTRIBUTING.md: fix broken triage link) - rust-lang#5424 (Incorrect suspicious_op_assign_impl) - rust-lang#5425 (Ehance opt_as_ref_deref lint.) Failed merges: - rust-lang#5345 (Add lint for float in array comparison) - rust-lang#5411 (Downgrade implicit_hasher to pedantic) - rust-lang#5428 (Move cognitive_complexity to nursery) r? @ghost changelog: rollup
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:
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
instead of
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 astatic 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
.The text was updated successfully, but these errors were encountered: