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 variants
I 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.
foo @
This may be swerving into refinement type territory, but I naturally wanted to not type the foo @ in the previous example:
match c {
Zero | One => println!("{:?}", c),
// ...I feel this is a pretty hidden and uncommon aspect of patterns, and it'd be nice to just be able to intuit the type based on the pattern without adding the explicit binding. That might even mean we could do:
if let Count::Many(..) = c {
println!("{}", c.0);
}| 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.
|
Good feature in general, with enough movitation, but detailed specification is very questionable. |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov What are the pain points? Usually the problem with type inference fallbacks is the interaction with the trait system, but we can collapse "variant inference variables" to the enum type on contact with the trait system AFAICT. |
This comment has been minimized.
This comment has been minimized.
This is definitely the design I'm leaning towards now. However, I couldn't find any concrete description of the problems with inference in the previous RFC other than "we still haven't figured out default type parameters". Are there problems with the existing version of default type parameters and type inference for enum variants now?
But "making sure there are no weird interactions" is a difficult goal: it's hard to rule out any at all, save for thinking about it collectively. I'm going to update the RFC to suggest using type inference, similar to the previous RFC, but with some slight ergonomic adjustments. |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 19, 2018
•
|
I have been thinking a lot about this RFC lately. I use to think that it was a good idea and would be the first step to sealed trait or sum-enum. Turns out I now think it goes against Rust design and is fixing the wrong problem. enum Either<A, B> { L(A), R(B) }Enums are tagged unions. From all I can read, I don't think people fundamentally wants tag to become type. They basically want to avoid the weird pattern where the tag is having the same name as your type and having to wrap the content of your variant in a struct: struct A(int);
struct B(int, A);
struct C(String);
enum MyEnum {
A(A),
B(B),
C(C),
...
}As mentionned by @Centril a good example of that is Also what I want is implement a trait Turns out all of this can be achieved with fairly simple macros. If we want that in the language, and I think it would be really nice, then we could just have some macros in the std lib like: #[union] Expr { Box, If, While, Cast }Which desugar to pretty much the current I am really failing to see any other reason why people would say that want the tag of the enum to become a type. I am really not in favor of this RFCs, I think it's focusing on fixing a problem which is not one (tags are not typed) and introduce unreasonable and unintuitive limitations. |
This comment has been minimized.
This comment has been minimized.
alexreg
commented
Dec 20, 2018
|
@thibaultdelor That solves one of the issues that motivates this RFC, but not all. For the issue it solves, see my pre-RFC. It would be a lot more elegant to incorporate a solution like that into the language... the Also, there is an argument "tag's do not have to be types", but equally tags can be types, and are furthermore a natural fit for types, so I'm not sure it's a strong case... |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 20, 2018
•
What does it not solve? I didn't mentionned all the examples of the RFC but from what I could see all the problems have as the root cause the lack of nice syntax to define a simple sum type. To me the RFC fails to identify this root cause.
Agree with that, also far more difficult in term of backward compatibility... Your RFC introduce some change in the language. My solution doesn't require any change to the language or stdlib. Can be just a crate.
We are not talking about just adding a type here, we are talking about changing the compiler and introducing concepts in the languages. This DOES have an impact on compexity, learnability. etc... Think about an enum being like struct MyEnum<T>{
tag: &str,
content: T
}That's what an enum is, with some extra compiler checks. tag is a field. All we are talking about is trying to hide the tag from the user and change the syntax of Rust so instead of defining a tag, with the same syntax, you actually define a type and hide the tag from the user. All about syntax. The same thing can be achieved with a macro. I like the macro, because it becomes clear to the user that no new concept have been introduced, just a better syntax for existing thing.
I don't see whats hard or counter intuitive about: if let MyVariant(o) = my_enum {
o.do_something
} |
This comment has been minimized.
This comment has been minimized.
alexreg
commented
Dec 20, 2018
For one, you can't do things like: fn foo(Enum::Variant t) { ... }
There are no significant backwards compatibility issues. And yes, this is the issue with all evolution of the language and all language RFCs, but the pros and cons have to be weighed up.
Changing the compiler is no big deal. Introducing new concepts and increasing complexity or obstructing learnability can be. I argue that this RFC (and my pre-RFC) actually make things more intuitive and learnable, and reduce the burden on the learner -- at least, for someone learning Rust as a first programming language and having no prior expectations about enums. Being able to use the names of existing types as tags makes a lot of intuitive sense to me at least.
Okay, "far less" is probably an exaggeration. I would however like to see this sort of thing baked into the language because it's really quite a small addition in surface area, the increase in syntax/keywords is essentially nil, and it avoids the use of proc macros, which slightly obfuscate things from the user. That said, I'm happy a proc-macro solution like this exists for now! It has a lot of utility. |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 20, 2018
•
|
Anticipating someone mentionning Scala. Scala enums are very very different from Rust ones not sure we can relate to it. @bchallenor mentionned Now, I have been doing Scala for 5 years now and I have never ever seen a function taking a The point being that |
This comment has been minimized.
This comment has been minimized.
An enum is a sum type: the disjunction of a collection of other types. The variants of the enum are the types over which the enum is disjunctive. This is a well-known concept in programming languages and provides direct motivation for treating the variants as types. With this attitude, the conceptual solution provided by this RFC is very natural (as evidenced by previous RFCs in similar veins). I think the main concerns are implementation-focused, rather than conceptual concerns. Your concerns seem to be centred around your understanding of what an enum is, but I suggest that this proposal will make a lot more sense if you think of them from the perspective of sum types. |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 20, 2018
That's exactly what I am saying... you don't need that at all. If you need variant to be a type, wrap a struct into an enum variant. Variants are flags only useful to deconstruct the enum. What you want to pass to a function is the content of the variant. |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 20, 2018
•
I was referring to the internal representation. For the user it's a sum type. For the compiler it's something with a tag attached to tell it how to treat it.
Nope, if you really want to talk theory and "well-known concept" an enum is a degenerate tagged union of unit types, used only for their flags. In theory we've got everything we need in Rust and all those concepts are represented. What matters is the syntax now. In practice, in Rust, an enum is a tagged union where the first part of the variant definition is the variant name (or tag) and the second part is the content ( I am arguing that we don't need to make the variant Name a type. That doesn't make sense conceptually. There will always be a variant name, whether it's hidden or not. What we want is to make it easier for the user and have the option of not specifying the variant name. That's what this macro does: And this doesnt have the weird side effects of having some kind of half-types that you can't implement. |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 20, 2018
•
|
Ultimately, I think what we want is those two pieces of code to be strictly equivalent: mod MyEnum{
struct A(i32);
struct B(i32, i32);
struct C(String);
}
enum MyEnum {
MyEnum::A,
MyEnum::B,
MyEnum::C
}and enum MyEnum {
A(i32),
B(i32, i32),
C(String)
}This RFC looks like is doing half the way to that. The second half being "how to allow From a user perspective, the benefit of this RFC is that you don't need to do struct A(i32);
enum MyEnum{A(A)};You can do: enum MyEnum{A(i32)};and then you can do pattern matching using So the real improvement is how you declare your enum, which as I say can be done with a macro and won't have the limitations. |
This comment has been minimized.
This comment has been minimized.
You can get around the problems with enum variants at the moment, sure — but they're not particularly ergonomic or natural. The change proposed here is a natural way to extend the type system minimally to support a common pattern in Rust code.
A tagged union is another name for a sum type.
I think if you read a bit more about sum types (the Wikipedia article you link to actually has a good explanation), you'll see that this concept precisely matches up with the interpretation of a sum type / tagged union / disjoint union / etc. It's a type
Note that anonymous enums are an orthogonal feature to this one (though they can complement each other).
This RFC does address one concern with code like that, but the aim of the RFC is not to make them equivalent (in fact, the RFC is specifically written so that they are not: see @shepmaster's comments above).
This kind of pattern matching (which is more evident in But regardless, this RFC isn't meant to solve "all enum-related problems". It is focusing on a very particular ergonomics shortcoming that provides cleaner patterns for dealing with enums (with a solid basis from the theory of sum types). I agree that there are other improvements that would be nice to have regarding enums, but I think they're orthogonal to the problem that's being tackled here. It's better to make smaller, incremental improvements than try to tackle all the problems at once. |
This comment has been minimized.
This comment has been minimized.
|
I've updated the RFC to propose a type-inference based approach to determining the type of variants/enums now, which is more in line with the previous RFC.
I've been giving it some thought and I couldn't figure out how variant inference might conflict or have weird interactions with the other kinds of inference (nor did a few others I asked). It could be the case that type inference is simply more well-established now than it was when #1450 was proposed, or I might not be being imaginative enough. Either way, that's one of the points of the RFC process, so I figure I'll leave it as an open question and if anyone can spot issues, we can address them — without anything concrete to go on now, though, it's hard to resolve that concern. I've also left a little summary of differences to #1450, but GitHub is not being happy sharing the RFC content at the moment, so I may update this with more differences once I can access it again. Regarding @shepmaster's desire for space-optimised variants, I think this is probably a fundamental tradeoff with performance. It would be nice to enable a space-optimised use case, but I think this could be left for a future RFC. This RFC doesn't commit to a representation, but suggests an initial one. This should leave us open to experiment in the future. (E.g. maybe we could have some kind of attribute to control this.) |
This comment has been minimized.
This comment has been minimized.
I've just had an interesting thought about this, though it may be slightly off-topic. Anonymous enums/sum types will presumably represent each of their variants as standalone types (as that's what they are), rather than repackaging them somehow. If there was then a built-in way to convert an |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 22, 2018
|
Meh, i am obviously explaining badly, because all the counter arguments are completely missing my point.... Just to make it clear, I am 100% in favor of, and really want to see happening, simple sum types as in The current RFCs build on that and try to bend the current syntax so it looks a bit more like your usual sum type.. Except that:
You could argue that it's a step in the right direction to which i would say that if you don't know what the final state, you don't know if it's the right step or if you just build and enforce things that you will want to get rid of. On the other side, a macro would solve most of the problems you are already solving and doesn't introduce new concepts. Rust is already complex and I am tired of explaining all the little quirks to my colleagues. This would be an other quirk which to me is bringing much less benefits than what people think in practice. Something like Haskell sum-type, or Scala sealed class, would make much much more difference difference to the language but that's not what this RFC is proposing. |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 22, 2018
|
I would be really keen on comparing a real world piece of code, one refactored by me, the other with this RFC. |
This comment has been minimized.
This comment has been minimized.
leonardo-m
commented
Dec 22, 2018
•
|
I feel that the design ideas of varkor here are better, I have had functions that return (or take) just an enum branch unconditionally. An example, here the insert() never returns None: http://rosettacode.org/wiki/Pattern_matching#Rust But feelings aren't enough to design a language and that isn't idiomatic Rust code (because it allocates too much), so I agree with thibaultdelor that it could be a good idea to compare how a more sizeable chunk of real world code could come out applying varkor or thibaultdelor ideas (one example could come from applying the ideas to parts of rustc source code, even if compiler-code is a rather special kind of code). |
This comment has been minimized.
This comment has been minimized.
Right, so you're looking for a more general syntax for algebraic data types that allow you both to define new types (à la enums at the moment) or use existing types as variants? Regardless of what an ideal future syntax would look like, the fact is that there is a syntax for enums at the moment and that's not going to go away, even if new ways of dealing with ADTs exists in the future. I think this enhancement to the existing enum syntax removes a papercut in a natural way (rather than being a "quirk"). For those that don't want to take advantage of this, there's no drawback to having this as a feature — existing patterns will continue to work — but it does solve a real problem now in a minimal way. I don't see that having new syntax or features for dealing with ADTs in the future is held back by implementing this proposal. Do you think that the proposal here is forwards incompatible or leads to confusing behaviour for type system extensions in the future?
I don't think this introduces new concepts, other than "enum variants are types", which is a simple concept to understand. Do you think there are other conceptual difficulties? I think many/most people would expect enum variants to be types (especially those coming from functional backgrounds). |
This comment has been minimized.
This comment has been minimized.
I'm confused by this. Rust For example, we can translate: enum Foo {
Bar(Alpha, Beta),
Baz(Gamma),
Quux
}directly into: data Foo = Bar Alpha Beta | Baz Gamma | QuuxThere is no semantic distinction here (aside from a lack of |
This comment has been minimized.
This comment has been minimized.
thibaultdelor
commented
Dec 22, 2018
•
|
@Centril As I have written, all the concepts in Rust are here, it's all about syntax. Depending on how simple is the syntax, you encourage different developer behaviors. To take your example you can argue that your translation is what this RFC would lead to, whereas Rust in the current state it would translate to : data Foo = Alpha Beta | Gamma | ()
This is fundamental in what I am saying. The type of the variant in current rust can be seen as the type of its content. It already has a type. This RFCs change the semantic without changing the syntax, which I am fine with. @varkor All the arguments I hear in favors are things like
The reason why people don't do it is because sum-types in Rust have a heavy syntax which discourage it. This RFCs, makes it slighty easier by changing the meaning of Basically, a slightly shorter syntax and being able to pattern match by type. (Arguably It doesn't make much difference to the user how it's matched) With a macro I could do: struct Bar(Alpha, Beta);
struct Baz(Gamma);
struct Quux{};
sum!(Foo, [Bar, Baz, Quux]);
// maybe create a subset too: sum!(FooSub, [Bar, Quux]);
// Pattern match
if let Bar(a) = my_foo {
println("{}", a.0)
} Many different option on what this macro does or its syntax, but here I guess it would generate: enum Foo {
Bar(Bar),
Baz(Baz),
Quux(Quux)
}This solves some problems, can be done right now and doesn't have limitations on the struct since you defined them explicitely. It's also FAR from being ideal and has this weird relation between the variant name and its type. I dislike how One of those 2 things would make me change my mind:
|
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? |
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!