Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upEnum variant types #2593
Conversation
varkor
added some commits
Nov 3, 2018
Centril
added
the
T-lang
label
Nov 10, 2018
This comment has been minimized.
This comment has been minimized.
alexreg
commented
Nov 10, 2018
|
Great work, @varkor. I've been looking forward to this for a long time. Just as a side-point, I'd love to follow this up with an RFC for the ideas in https://internals.rust-lang.org/t/pre-rfc-using-existing-structs-and-tuple-structs-as-enum-variants/7529 once this gets implemented in nightly (or perhaps even before). Since you've worked on this, would appreciate your thoughts at some point. |
This comment has been minimized.
This comment has been minimized.
bchallenor
commented
Nov 10, 2018
This is possible in Scala - as in your example, Left and Right are subtypes of Either, and can be referred to independently. Coming from Scala, I miss this feature in Rust, and I am fully in favour of this RFC. |
This comment has been minimized.
This comment has been minimized.
Ah, great, I'll add that in, thanks! |
This comment has been minimized.
This comment has been minimized.
|
I think I'm in favor of the proposed functionality and semantics here. Where I'm stumbling is the nomenclature/terminology/teachability(?); it's not clear to me that "introducing a new kind of type: variant types" is the best description of this. In particular, precisely because this proposal feels so lightweight compared to previous ones, it doesn't really "feel" like what we're doing is adding a whole new type kind the way structural records or anonymous enums would be doing. It sounds like it could be equally well described as doing the "duplicating a variant as a standalone struct" workaround automagically, so those extra structs are just always there (except they get a specific layout guarantee and different conversion syntax that regular structs wouldn't get). Is there some detail I overlooked that makes this clearly not a sugar? I'm guessing this is at least partially ignorance on my part because
makes it sound like "variant types" are an actual thing with their own special properties that no other kinds of types have, and I just have no idea what that would be (since being autogenerated, having a certain layout guarantee and different conversion syntax seem like "surface level" properties that aren't really part of the type system per se). Maybe I just need to see some more examples of how these types behave? |
This comment has been minimized.
This comment has been minimized.
leonardo-m
commented
Nov 10, 2018
•
|
Nice RFC.
Is code like this still allowed, or is the compiler going to tell me that the Sum::B(b) branch of the match is impossible and needs to be removed?
Both options have advantages and disadvantages. |
This comment has been minimized.
This comment has been minimized.
This is a good question — I'll make note of it in the RFC. Although matching on variant types permits irrefutable matches, it must also accept the any other variants with the same type — otherwise it's not backwards compatible with existing code.
It's quite possible there's a better way to explain this. They are essentially as you say, though they act slightly differently from structs (on top of the points you made) in the way they are pattern-matched (as above in this comment) and their discriminant value. I thought it would be clearer to describe them as an entirely new kind of type, but perhaps calling them special kinds of structs would be more intuitive as you say. I'll think about how to reword the relevant sections. |
varkor
added some commits
Nov 10, 2018
varkor
force-pushed the
varkor:enum-variant-types
branch
from
14a6e83
to
2b00420
Nov 10, 2018
This comment has been minimized.
This comment has been minimized.
|
This was previously proposed in #1450. That was postponed because we were unsure about the general story around type fallback (e.g, integer types, default generic types, etc). Enum variants would add another case of this and so we wanted to be certain that the current approach is good and there are no weird interactions. IIRC, there was also some very minor backwards incompatibility. This RFC should address those and issues, and summarise how this RFC is different to #1450. For the sake of completeness, an alternative might be some kind of general refinement type, though I don't think that is a good fit with Rust. I'm still personally very strongly in favour of this feature! The general mood on #1405 was also positive. |
This comment has been minimized.
This comment has been minimized.
I'm not sure that would need to be at odds with variant types, if Rust ends up with refinement types I expect variant types to be refinements of their enum. |
This comment has been minimized.
This comment has been minimized.
|
First, irrespective of what happens with the RFC; I am of two minds and a bit torn about the proposal here.
(Feel free to integrate any points that you found relevant into the text of the RFC)
(Aside, but let's not go too deeply into this: I personally think that refinement / dependent typing is both a good idea, a good fit for Rust's general aim for correctness and type system power for library authors -- and RFC 2000 is sort of dependent types anyways so it's sort of sunk cost wrt. complexity -- the use cases for dependent/refinement types are sort of different than the goal here; With dependent types we wish to express things like
I agree; I think you can think of variant types in the general framework of refinement / dependent types; type FooVar = { x: Foo | x is Foo::Variant(...) }; |
Centril
added
the
A-data-types
label
Nov 10, 2018
This comment has been minimized.
This comment has been minimized.
burdges
commented
Nov 10, 2018
|
We do want formal verification of rust code eventually, and afaik doing that well requires refinement types. I'm not saying rust itself needs refinement types per se, but rust should eventually have a type system plugin/fork/preprocessor for formal verification features, like refinement types. I do like this feature of course, but ideally the syntax here should avoid conflicts with refinement types. |
This comment has been minimized.
This comment has been minimized.
Are there any such conflicts in your view? |
This comment has been minimized.
This comment has been minimized.
|
(Or to elaborate; if there are any conflicts with the RFC as proposed with refinement typing, then stable Rust as is has that conflict since the RFC does not introduce any new syntax...) |
This comment has been minimized.
This comment has been minimized.
ExpHP
commented
Nov 10, 2018
•
So, if I understand correctly, all existing code that uses enums now have coercions all over the place in order to ensure they continue functioning? I'm really not sure this works... let mut x:
x = None;
// At this point the compiler knows the type
// of x is Option<?0>::None.
// But Option<_>::Some cannot be coerced to None
x = Some(1); // type error? |
This comment has been minimized.
This comment has been minimized.
leonardo-m
commented
Nov 10, 2018
•
I think in theory the type system should infer x to be of type But I think we need a formalization of the involved type system rules, to assure soundness, before implementing this proposal... |
This comment has been minimized.
This comment has been minimized.
|
I would rather frame this as follows:
|
This comment has been minimized.
This comment has been minimized.
leonardo-m
commented
Nov 10, 2018
•
While I don't dislike LiquidHaskell-like refinement typing, lately for the future of Rust I prefer a style of verification as in the Why3 language ( http://why3.lri.fr/ , that is also related to the Ada-SPARK verification style). We'll need a pre-RFC for this. |
This comment has been minimized.
This comment has been minimized.
leonardo-m
commented
Nov 11, 2018
|
I hope this syntax is also supported (I suggest to add it to the RFC):
A question regarding the ABI: is the print_a1() function receiving the Sum discriminant too as argument? And in future it could also be supported the more DRY syntax (I think suggested by Centril):
You could also add a new (silly) example to this RFC that shows the purposes of this type system improvement:
With this improvement you can write instead:
Then you can define a list_head_succ() function that returns the head of the result of prepend() without a unwraps or Option result:
For the common case of integer intervals for Rust I sometimes prefer a shorter and simpler syntax like:
|
shepmaster
reviewed
Nov 11, 2018
"Overhead"I'm mostly interested in this RFC from the point-of-view of "enums of lots of standalone other types". The biggest example I have is the AST expressed in fuzzy-pickles, a Rust parser which uses this pattern extensively: pub enum Item {
AttributeContaining(AttributeContaining),
Const(Const),
Enum(Enum),
// ...Unfortunately, I don't see this as being a large win for such a case due to the "forced overhead" of each enum variant still being the same size as all the other variants. It's an understandable decision, just not one that I see as helping as much as it could. This is mentioned in the alternatives section, but I want to make sure the point is reiterated. Multiple variantsI didn't see any mention of if multiple variants would be supported: #[derive(Debug)]
enum Count {
Zero,
One,
Many(usize),
}
fn example(c: Count) {
use Count::*;
match c {
x @ Zero | x @ One => println!("{:?}", x), // what is the type of `x` here?
x => println!("{:?}", x),
}
} It may also be worth explicitly calling out what the type is for those catch-all patterns as well as in cases of match guards.
|
| and `impl Trait for Enum::Variant` are forbidden. This dissuades inclinations to implement | ||
| abstraction using behaviour-switching on enums (for example, by simulating inheritance-based | ||
| subtyping, with the enum type as the parent and each variant as children), rather than using traits | ||
| as is natural in Rust. |
This comment has been minimized.
This comment has been minimized.
shepmaster
Nov 11, 2018
Member
I'm a fan of the proposed style, but it might be worth stating why Rust the language wants to dissuade this pattern.
| - Passing a known variant to a function, matching on it, and use `unreachable!()` arms for the other | ||
| variants. | ||
| - Passing individual fields from the variant to a function. | ||
| - Duplicating a variant as a standalone `struct`. |
This comment has been minimized.
This comment has been minimized.
shepmaster
Nov 11, 2018
•
Member
I disagree that this goal is going to be as widely achieved by this RFC as I would like due to the following point:
the variant types proposed here have identical representations to their enums
That means that if I have an enum with large variants:
enum Thing {
One([u8; 128]),
Two(u8),
}Even the "small" variants (e.g. Thing::Two) are still going to take "a lot" of space.
This comment has been minimized.
This comment has been minimized.
eddyb
Nov 11, 2018
Member
If space is a concern then we could have it so variant types only convert to their enum by-value, so e.g. a &Thing::Two wouldn't be a valid &Thing.
That's weaker than something more akin to refinement typing, but maybe it's enough?
This comment has been minimized.
This comment has been minimized.
Centril
Nov 11, 2018
Contributor
@eddyb I think that's already the case; the RFC doesn't state anywhere, as far as I can tell, that &Thing::Two is a valid &Thing. Also note that the RFC explicitly states that Thing::Two and Thing having the same layout is not a guarantee so we could change the layout to be more space efficient.
This comment has been minimized.
This comment has been minimized.
No, it would not.
data UIdent $(LIdent)* = $(UIdent $(Type)*)|*Furthermore, the translation to The semantically accurate transformation (forgetting about recursive types) of a Rust enum TypeName {
VariantName(Type, ..., Type),
...,
VariantName(Type, ..., Type),
}into: data TypeName
= VariantName Type ... Type
| ...
| VariantName Type ... Type
In Haskell as well: data List a = Nil | Cons a (List a)
instance Functor List where
fmap f list = case list of Nil -> Nil; Cons x xs -> Cons (f x) (fmap f xs) |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 22, 2018
|
Indeed you are right. Doesn't really change my point. |
This comment has been minimized.
This comment has been minimized.
This works only when your variant contains at most one type (e.g.
I'm not strongly opposed to allowing impls on variants; I was intentionally being conservative. If there are strong feelings in favour of permitting impls, I can add it to the RFC. I'll add it as an unresolved question. |
This comment has been minimized.
This comment has been minimized.
alexreg
commented
Dec 22, 2018
I personally would like to see it very much. Otherwise variant types feel like "2nd class" types from the start. It can be handled as part of specialisation, right? |
This comment has been minimized.
This comment has been minimized.
This presumes that there will ever be specialization. Last I paid attention, there was still no satisfactory conceptual foundation that people felt was good enough. |
This comment has been minimized.
This comment has been minimized.
alexreg
commented
Dec 29, 2018
|
@shepmaster It's been figured out. Just waiting on Chalk foundations I believe, which are almost there. http://aturon.github.io/2018/04/05/sound-specialization/ |
This comment has been minimized.
This comment has been minimized.
|
I'm really glad to see this proposal. Coming from someone writing AST types, I definitely would like to see impls allowed on individual variants in order to allow some, but not all variants, to contain some functionality. I think I disagree with the RFC author that we necessarily want to discourage behaviour switching on enums---it's a very standard thing with some kinds of subtypes because you already have the implementations handy. (This definitely goes up with the idea of a fully general struct/enum unification approach, but assuming that's a little out of reach, I'd still like to leave the door. It may even allow considering using the trait system for the "arbitrary subset" refinement approach in the future, and I'll try to illustrate that idea at the end of the post.) My musings there eventually lead me to think that we should consider forcing all the variants to have the same size/layout so that they are reference-compatible, because I think that there's a decent chance the differently-sized version has an alternative solution. Rationale for impls In my simple (but a little contrived) example, suppose that I'm designing a language where enum Decl {
Named(NamedDecl),
Pragma(...),
...
}
struct NamedDecl {
name: String,
inner: InnerNamedDecl,
}
enum InnerNamedDecl {
Func(FuncDecl),
Var(VarDecl),
}This is really ugly though, because I have a ton of layers of indirection and abstraction here. Additionally, it has a major usability cost: even though An alternative could be to simply write partial functions on Instead, traits should be used to factor out common functionality, right? So we want something like this: enum Decl {
Func(FuncDecl),
Var(VarDecl),
Pragma(PragmaDecl),
...
}
struct Func(...);
struct Var(...);
struct Pragma(...);
trait NamedDecl { ... }
impl NamedDecl for Func { ... }
impl NamedDecl for Var { ... }This is a good approach. You can write functions that take a Banning impls on the individual variant types would effectively force a user to either abandon this pattern (which I think is the best of the options available) or forego the benefits of variant types altogether. Unless there's a better pattern that I'm missing, I think this argues very strongly in favour of impls on individual variant types. Alternative Matching Nit This approach does have one small weakness though, which is on alternative-match patterns: fn foo<D: NamedDecl>(d: D) { ... }
fn bar(d: Decl) {
match d {
d @ (Decl::Func(_) | Decl::Var(_)) => foo(d)
...
}
}Morally, it feels like this "ought" to work---within the branch of the match, even though you don't know which variant And Now Alercah Derails the Thread I generally think that it's worth considering the future direction of things, especially since I see this feature careening on a collision course with sealed traits. #2618 is a new RFC proposal that illustrates how this direction is very much unconsciously in people's minds. Sealed traits, of course, are a thing that keeps coming up and nobody has really bothered with a serious RFC because there's easy-enough ways to simulate them. But sealed traits actually have a significant implication on the type system, because a sealed trait effectively creates a finite subtype. Consider my example of the trait fn destructure<D: NamedDecl>(d: D) {
match d {
FuncDecl(...) => ...,
VarDecl(...) => ...,
}
}In other words, we can just use the This could work on We also arrive at something similar if we come at the original enum problem from the other direction, and consider the idea of allowing enum variants to be nameless. Looping back again, we could imagine something like: struct FuncDecl { ... }
struct VarDecl { ... }
struct PragmaDecl { ... }
inline enum NamedDecl {
FuncDecl,
VarDecl,
}
inline enum Decl : NamedDecl {
PragmaDecl,
}where the variants have no actual identity of their own. (This is arguably a more elegant solution to the original problem even, and possibly worth considering as an alternative to the original proposal. If we want to bikeshed syntax, perhaps I'd have to think through the implications carefully, but I think offhand that the only major difference between the sealed trait approach and the 'inline enum' approach becomes that of whether the variants all share the same layout or not---the In other words, enums and sealed traits share the same basic properties of being sum types. But enums have a shared layout and nominal variants; sealed traits have varied layout and unnamed variants. Maybe aiming for a solution that unifies this all together by allowing selecting between nominality and layout separately is ideal. |
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Jan 2, 2019
What exactly do you mean by "reference compatible" here?
I might be missing something, but doesn't anything dynamic need to perform some sort of runtime check to get to the correct function/type(/anything associated with them)? A pattern match is a comparison of the enum discriminant against a constant, and a branch, or an indirect branch indexed by the discriminant (depending on how the compiler happens to codegen it), modulo optimizations. But a method call through a trait object isn't free either: it's an offset against the vtable pointer and an indirect call (again, modulo devirtualization).
Isn't that particular use case of ASTs solved by just implementing the trait on the member structs, though?
But with newtype variants around structs, you still have the concrete types of the wrapped structs, so you can just call different methods (possibly from different traits) on them. I'll explain this below in more detail, but I think if you want to treat two types differently, you simply shouldn't try to achieve that by means of a common trait. Just use the concrete types, or perhaps different traits.
Going from a trait to a specific type induces feelings of object-oriented "downcasting" in me. (No surprise, the hack that is known as I've long enjoyed Rust's enums because of the beauty, simplicity, and obvious behavior of pattern matching, and I'd really prefer not to spoil it by having arbitrary downcasts over (partial) subsets of variants. I see how this might result in some writing convenience, but it's awful for readability (which, I argue, should be weighted a lot more in evaluating language features. We spend much more time reading code than power-typing). By the way I've done a lot of AST manipulation in the past too, and this never was a real issue. Yes, it means that when the represented syntax changes, you have to restructure the AST nodes associated with it. I'd consider that to be inevitable by nature, rather than a "problem". |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Jan 2, 2019
•
I see at least two reasons:
I have always advocate for "downcasting is a code smell" in other language. On the Rust side, unlike most language, things are much different and concrete types have very different properties than traits. |
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Jan 2, 2019
This can already be achieved by just matching on the enum once and using the contents of its variants directly. By the way, traits as bounds to type parameters result in static dispatch. Unless you specifically ask for a trait object, there's no dynamic dispatch going on by default.
That is not true anymore because there is Of course traits are different from concrete types – that's kind of their raison d'être. Generic programming is a quite high level of abstraction, indeed – so people not familiar with it might need some time until they get used to programming against abstract interfaces rather than concrete types. I wouldn't say that this means that "traits are harder to deal with" in general, nor that this would warrant conflating certain, very different aspects of the language. |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Jan 3, 2019
Well then you are not using traits at all, that's a completely different story. You were making a point with abstraction and traits.
Traits have additional restriction, can be monomorphised, require knowledge about trait objects, require to be boxed in some scenario... |
This comment has been minimized.
This comment has been minimized.
I mean that
Yes, it does. But here I meant that you're doing this repeatedly because it's done on every single method, rather than doing it only once: if you pattern match on an enum to destructure it and then access the fields of the variant, you don't need to do the dispatch every time you access a field. Trait objects basically do have to do dynamic dispatch each time, but it's nicely hidden by the compiler.
On the variant types you mean? The RFC proposes to ban that.
Yes, you will. But sometimes you'll have a function which takes a
I dislike downcasting too, but destructuring an enum into its variant is not really different from downcasting, especially over a sealed trait (where you know every possible concrete type). I think the moral argument here is about when you expect consumers of a trait to be (morally) parametric. For enums you generally don't, but for unsealed traits you generally do. For a sealed trait, I think it starts to blur the line---and I think that taking advantage of that blurring in language design is valuable. |
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Jan 3, 2019
Can you please provide a more specific example? The quote above seems to go against your former argument. I suspect we might be talking about different things… What I meant is, given an enum:
you can implement a method on it, which calls into methods on the variants' associated data:
This isn't any worse, in terms of number of runtime checks, than:
No, I meant on the types of the associated values wrapped in each newtype variant.
Well, then we just disagree here. I think if consumer code wants to behave differently based on concrete types, an enum with public variants is a perfectly fine solution, and such an enum around the different types should be provided. There is no point in consuming a trait if its abstraction capabilities are thrown away because one ends up inspecting the concrete type anyway. That only results in unnecessary complication and an unclear mixture of concepts, which are hard to understand because they belong to neither approach (concrete types or abstract interfaces). To clarify, I'm not saying that switching on an enum is in itself bad; rather, switching on an enum and taking advantage of the heterogeneous types, while pretending to program against a homogeneous interface is bad.
Indeed, it isn't from a technical point of view. However,
…I disagree with this statement too. This is exactly what I'm trying to avoid, as mentioned in the previous point. |
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Jan 3, 2019
I am well aware that
That is really vague; encapsulation is a form of/tool for abstraction. I also fail to see why hiding a concrete type under an interface doesn't qualify as "abstraction". But I'm not particularly interested in arguing over definitions; I was merely falsifying your (incorrect) claim that a type implementing a non-object-safe trait can't be returned.
Again, these claims are very general and hard to concretize. Yes, generic functions and types with trait bounds can be monomorphized. Is that bad? Yes, traits can and sometimes need to be boxed, just like other types. Is that bad? When you make a trait object, it becomes a concrete type and can for all purposes be treated as one. Also based on this, I'm not sure about the "additional restrictions". You're really making an apples to oranges comparison here. Traits, generics, and trait objects all have their specific purpose, and they are used differently from (concrete, unrelated) types. I don't really see that as a problem. They are distinct language features solving different problems, which is why they all exist and are all useful in different ways. |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Jan 3, 2019
•
Are you actually suggesting that it rarely occurs?!? Doing something like the following is pretty common IMO : if something {
return this;
} else {
return that;
}(where
Which problem?!? Which proposal?!?
You are arguing with a straw-man... A non object-safe trait can't be return as such. If you know the concrete type then you can but it's not much different conceptually than returning the concrete type and it's not my point. When talking about abstraction, the case where you abstract over a single type is not really the most relevant one.
Who said it was? I said harder to deal with, because it has technical implications and limitations and forces you to think (like boxing) about concepts that you don't have to with concrete types.
You can't always do that, that was my point. I am going to stop replying, the discussion isn't constructive. |
This comment has been minimized.
This comment has been minimized.
H2CO3
commented
Jan 3, 2019
No need to yell. I'm not suggesting that this "rarely occurs", it was just unclear what you precisely meant by "knowing the concrete type". Your example is legitimate. However, it still does need some sort of runtime check. If the returned concrete type depends on a parameter that is only known at runtime, there is no way a runtime check could be avoided.
Agreed. |
varkor commentedNov 10, 2018
•
edited
Enum variants are to be considered types in their own rights. This allows them to be irrefutably matched upon. Where possible, type inference will infer variant types, but as variant types may always be treated as enum types this does not cause any issues with backwards-compatibility.
Rendered
Thanks to @Centril for providing feedback on this RFC!