Revisit: Types for enum variants

As I was reading the blog for Rust stable 1.20, I saw the associated constants and wondered how it would work for enums. Sadly in order to have a different constant for each variation of an enum they must be treated as types, which was brought up by https://github.com/rust-lang/rfcs/pull/1450 which was postponed.

So, I feel that it has been a decent amount of time since the postponing, and it was said that it would be revisited. So I just wanted to bring it up here for revisit. I’m not sure if there are any specific things blocking this as of now.

3 Likes

This comment mentions

this is blocked on further investigating default type params

Which I think is elaborated in this one.

Skimming, I don't think inference fallback stuff is in a very different position from a year ago.

Well dang, might see if there is anything I can help with

It’s also possible that it could be approached from the other direction, making enums out of nominal types directly. (Along the lines of the IpAddr enum.) If that got automatic .into() impls it might avoid the “is the the variant or is it the enum” typing problems. I could imagine a form of that that would be incredibly useful for error types.

I’ve seen the IpAddr enum style used a lot and I myself use it aswell. It’s a decent work around, it just doesn’t look that nice when you have longer names.

Right, but maybe there’s a solution here aping how we have structs with field names and tuple structs.

Strawman:

enum IpAddrAlt ( Ipv4Addr, Ipv6Addr );
impl From<Ipv4Addr> for IpAddrAlt {
    fn from(x: Ipv4Addr) -> Self { x as Self }
}
impl From<Ipv6Addr> for IpAddrAlt { ··· }
match foo {
    v4: Ipv4Addr => ···,
    v6: Ipv6Addr => ···,
}
if let x: Ipv6Addr = foo { ··· }

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.