So then the only changes to existing code would be:

1) Wrapping the enum definition in a macro and giving it a type.
2) Changing `foo as bar` to `foo.to_bar()`.

Is this correct? Here's an example that I've modified with the new syntax:
https://gist.github.com/bstrie/5064858 . It looks pretty good to me. Is the
`foo.to_bar()` approach less efficient than casting?


On Fri, Mar 1, 2013 at 12:18 AM, Patrick Walton <[email protected]> wrote:

> Hi everyone,
>
> There is currently a feature whereby custom enum variant discriminants can
> be specified:
>
>     pub enum Foo {
>          Bar = 1,
>          Baz = 2,
>          Boo = 3,
>     }
>
> The sole use of this is casting from the enum to an int:
>
>     println(fmt!("%d", Bar as int)); // prints 1
>
> This feature currently doesn't really meet people's needs. Chief among the
> issues are:
>
> 1. Only ints are supported, but many enums want to correspond to different
> sizes of types.
>
> 2. Only casting *from* an enum value is supported, not casting *to* an
> enum value. Casting to an enum value is not supported because this is a
> partial operation.
>
> 3. There is no way to have multiple values correspond to a single enum
> variant.
>
> We could extend the syntax to support these in some way, but I'm
> personally leaning toward just moving the machinery into a syntax
> extension. You could write something like:
>
>     numeric_enum!(Foo : uint {
>         Bar = 1,
>         Baz = 2 | 3,
>         Boo = 4
>     })
>
> Then the compiler would generate functions like:
>
>     Foo::from_uint(uint) -> Result<Foo,()>
>     Foo::to_uint(self) -> uint
>
> One could also imagine a similar syntax extension for bitfields.
>
> Besides the obvious advantage that it simplifies the language, we can make
> the `from_foo` type return a proper Result (or use the condition system)
> without building Result into the compiler.
>
> Thoughts?
>
> Patrick
> ______________________________**_________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to