Nice, for future consideration: “Control over layout and discriminators”:
I’m sure @nate might find an good use of this related to Optional and Niche ![]()
Yep! I was about the raise the concern for not having “tuple-cases” but it seems like this will be supported which is great to see.
I believe this also helps quell @dmitry_salin 's concerns around the Rgb verbosity since you could then write the struct as such:
struct Rgb: ...
enum Color:
case Red
case Rgb(Rbg)
Exactly - “tuple-case” is a way to assign an alternative name to a type within the context of enum. In some cases this is redundant, but acceptable for the sake of simplifying the implementation of enums.
Hi all, long time no see. I have a lot of thoughts on enums, especially regarding pain points I’ve seen in other languages, and how Mojo might resolve them. For example, see this issue (pain point) in the Rust community. There’s a noteworthy comment from Niko Matsakis (Rust designer) in the replies:
We all agree that it is a common, and annoying, pattern in Rust today to have to make a struct for every enum variant and then just have the enum wrap those structs. This gives you the ability to have a “type for an enum variant”, but is annoying and inconvenient. So, for those reasons, we would love to see forward motion on this proposal. However, we also feel that this is striking at a fairly core part of the language, and there isn’t anyone on the team who has the bandwidth to actively liaison this effort.
Mojo has the opportunity to address these pain points right from the start, rather than trying to patch enums after they’ve shipped. So IMO it’s worthwhile treading carefully here, and pulling together the learnings from other language communities, before we commit to any particular design.
UPDATE: The remainder of this post covers my early thoughts on design alternatives for enums. I no longer think the ideas presented below are worth investigating. See my later posts for my current perspective.
Early thoughts
One direction worth exploring is whether Mojo actually needs a dedicated enum construct in the first place, or whether it’s sufficient to offer a new protocol on ordinary structs (a la Chris’s SumType trait), to enable structs to serve as the foundation for pattern matching.
Basically, I’m wondering whether we can evolve Variant (docs) into a fully capable “sum type” that supports pattern matching, and whether we can make it ergonomic enough to make enum declarations unnecessary. Python already has a nice vertical bar syntax that makes sum types easy to read and write (e.g. x: Int|None) and in Mojo this could desugar to Variant[Int, None], or something similar. A recursive list type could be declared simply as comptime LinkedList[T: Copyable] = None|Tuple[T, Box[LinkedList[T]]. This syntax happens to be more concise than Chris’s linked list enum posted earlier in this thread, but the primary benefit is that we didn’t need a dedicated enum syntax to define this type. All we need is Mojo’s existing type alias feature (and support for recursive aliases).
If we pursue this direction, when someone learning Mojo asks “how do I get sum types”, we can tell them “Mojo does things the Pythonic way, using (tagged) unions |”. So there’s a clean story we can tell; we wouldn’t be doing anything that’s weird/unfamiliar to the average programmer.
The last thing we’d probably want is a succinct syntax to declare zero-sized structs, to make simple enums like “red/green/blue” easy to write. Perhaps just being able to declare structs in bulk, e.g. struct Red, Green, Blue: pass would suffice. Or maybe a syntax like comptime Color = struct Red | struct Green | struct Blue.
My thoughts on this topic go a lot deeper. When I have time (probably this weekend), I’ll come back to this thread and write up a more detailed post. In the meantime, I’m keen to hear what people think about this design direction.
-
I think it is possible to implement something special for a “sum type” that can be constructed from variadic parameter or parameter union expression like
A | B | C. -
It is necessary to provide a mechanism for controlling alignment, trait conformance, etc., for a specific enum.
For example:
@align(64)
enum Color(Defaultable):
case Red
case Rgb(Rgb)
It seems that comptime declarations and a single Variant type cannot provide this.
It seems that
comptimedeclarations and a singleVarianttype cannot provide this.
I don’t see any roadblocks. Here’s what I’d write, once struct extensions are implemented:
# Declares Color as an alias for Variant[Red, Green, Blue]
comptime Color = struct Red | struct Green | struct Blue
extension Color(Defaultable):
def __init__(out self):
self = Self(Red()) # delegates to Variant.__init__
This struct extension on Variant will be valid (i.e. satisfy the coherence rules) as long as Mojo adopts rules similar to those of Carbon. I posted info about Carbon’s rules here. (They were explained in a talk.)
Regarding alignment (@align): I’m not sure how it is modelled today, but I could imagine it also boiling down to a trait implementation.
Here’s another slight variation worth considering. What if this syntax:
enum Color:
struct Red # zero-sized struct with a default constructor
struct Green
struct Blue
Desugared to comptime Color = Enum[struct Red, struct Green, struct Blue].
So there is still only one core type for sum types (Enum, a.k.a. Variant), which can be defined in the standard library, and extended by way of extensions.
I’m still not convinced the enum sugar is actually necessary (| might be enough), but if we decide that it has real utility, we can still explore different options for what it desugars to. Chris’s proposal assumes that this syntax declares a brand new nominal struct, whereas I am wondering whether it should just desugar to an Enum/Variant struct.
If we still go for the current approach, then I don’t undestand why we’re deviating from current Mojo syntax at all.
why isn’t this
enum Color:
case red
case green
case blue
case rgb(r: Int, g: Int, b: Int)
just this?
struct Color(Enum): # or maybe we do keep the `enum` keyword
var red: NoneType # or maybe we allow `...` here?
var green: NoneType
var blue: NoneType
var rgb: Tuple[Int, Int, Int] # or `case rgb: RGB` if an RGB type exists
Something to also think about is that by making enum it’s own thing that gets auto-sintesized constructors, we’re kind of losing the control of what e.g. implicit constructors we allow for a given type. I hope we get that back in the future.
I’m generally +1 on Nick’s idea of improving Variant and folding enums into it.
The proposal has got some significant updates.
SumTypeis renamed toEnumLike- Any struct that conforms to
EnumLikecan participate in pattern matching - The syntax for tuple cases has been reworked:
enum Color:
case red
case green
case blue
case rgb(Int, Int, Int)
immas default convention- and much more, like easier struct cases:
enum Color:
case red
case green
case blue
case rgb(Rgb)
What are the advantages of using a single struct for all enums? In any case, different enums will be different types. However, there is a problem: decorators are applied to struct declarations, and something else applicable exclusively to such declarations might appear. Furthermore, changing anything in this single struct will affect all enums.
FWIW, if you didn’t read that far, the end of the proposal ponders a future direction where we support anonymous enums. It also points out a few of the concerns / open questions that would have to be resolved before doing so.
Let’s get the basic proposal implemented and landed and get experience with it, then we can assess further extensions.
-Chris
I generally prefer jumping over nested branching. I have not read the whole proposal since it’s a lot of text.
I hope mojo finds 2 ways to fix rust pattern matching:
A: borrow checker not playing nice with enums
B: needing ∞ match/if let sugar because control flow feels unnatural.
So destructering ( currently just hardcored to Tuplen) will work with any comptime iterator like python and js?
Array, range…
Yeah I did see that section, although I only glanced at it. I’ve started to put together a more detailed response to your enum proposal. I will probably post it tomorrow. So far I’ve mostly been reading historical design discussions from other language communities (e.g. Rust and Swift) to make sure I haven’t missed any important footguns/lessons that can be used to improve Mojo’s enum design.
I do intend to offer feedback on your MVP enum design (the one you’re currently implementing); I’m not going to propose fancy extensions or syntax sugar. My main concern is that there are some design details in your MVP which IMO could negatively impact Mojo’s future evolution, especially when Mojo gets Python/Swift-style classes. I just want to make sure the enum foundation is solid, since it’s easier to change things now, before the MVP is shipped/stablised and has a large user base.
FWIW, I like the general direction of your proposal. It is only a few details that worry me.
Anyway, sorry about all this foreshadowing I’m doing, I don’t think it’s particularly helpful. A proper response is coming tomorrow!
We had a lot of discussion at the end of last year. I am glad mojo is finally moving forward. My prefered solution (with kotlin like smart casts ) depends on how much compiler complexity and compile times we are willing to accept. Then there is of course the syntax bikeshedding debate ( if var vs is/as ). I think there are also some open issues related to this, which could also be used as reference.
I am just guessing. I am not familiar with the mojo compiler. My assumptions are just based on the premise of MLIR and “better infra”.
Alright, I’ve now had enough time to study @clattner’s proposal and formulate a proper response. Here’s my feedback.
First, I want to make clear that I agree with the vast majority of your proposal Chris. In particular I agree that enums should be more or less “just structs”, and support method declarations and trait conformances. I also agree that enums should introduce brand-new nominal types. Despite my earlier posts, I now agree with others that desugaring enums to Variant is probably not a suitable approach, for a few different reasons.
My main concern about Chris’s proposal (as currently written) is that it leads us away from being able to treat cases as structs. To be more precise: I think we want the ability to treat an entire case as a struct, including its embedded discriminant. Treating just the payload of a case as a struct (where the payload carries no discriminant) closes some doors that IMO we should be leaving open. In the rest of this post I will explain what these “doors” (opportunities) are, and why we might want to pursue them.
But first I’ll show everyone what I think enums should look like. It so happens my design is almost identical to the one that Owen proposed in his most recent post. This is a happy accident: until just now I had no idea what Owen wanted enums to look like; I hadn’t seen his post. I’ll take that as a sign that this design is especially promising.
My proposed design
For “simple” enums where each case is a value without a payload, I am in 100% agreement with Chris’s current proposal. For example, here’s how I’d define a 3-value Color enum:
enum Color:
case Red
case Green
case Blue
Note that I am capitalizing the cases. This is because I am treating them as (singleton) types. Like in Chris’s proposal, I would expect that Color.Red can be used as a value, but I would also like to reserve the possibility of using Color.Red as a type. (Typically as part of a more complicated type expression.) You can think of this as analogous to how None is used in both Python and Mojo: you can write -> None as a return type, but also write return None.
For “complicated” cases that need to carry data, I would like us to adopt the case struct syntax that Owen proposed earlier:
enum Color:
case Red
case Green
case Blue
@fieldwise_init
case struct RGB:
var r: Int32
var g: Int32
var b: Int32
@fieldwise_init
case struct HSL:
var h: Int32
var s: Int32
var l: Int32
The syntax and capabilities of a case struct are identical to those of an ordinary struct: you can add decorators, define fields and methods using the standard syntax, and even (some day) conform to traits:
enum File:
case struct Open(Writer): # Open files can be written to
...
case Closed # You cannot write to a closed file
Notice how we’re using parentheses to delimit trait conformances, as we do for ordinary structs. This syntax is at odds with Chris’s proposal, wherein parens are used to delimit the payload tuple, e.g. case RGB(Int32, Int32, Int32). This is one example of a door that I don’t want us to close unnecessarily. IMO we should reserve the use of parentheses next to a case’s name to list its trait conformances, even if the enum MVP doesn’t support that.
Case structs are constructed using a standard constructor call, with the exception being that the return type should (by default) be inferred as the enum’s type:
var c = Color.RGB(100, 240, 70) # c has type `Color`, not `Color.RGB`
Some day in the distant future, likely when Mojo has class hierarchies and subtyping, we can explore more sophisticated alternatives for how to infer the type of c. For example, maybe it resolves to the most specific type that type-checks. But in the short-to-medium term, the above behaviour seems like the only sensible choice.
One important detail worth noting is that a case struct is not semantically equivalent to the “payload structs” discussed in Chris’s proposal. Case structs have an (implicit) embedded discriminant, whereas Chris’s payload structs do not. More generally, the size and alignment of each case struct matches that of its enclosing enum. I will explain why this distinction is important shortly.
This pretty much summarises what I would like the enum MVP to look like. The biggest difference is that we are modelling enum cases as structs. Apart from that, everything else should work more or less the same as in Chris’s proposal. Pattern matching, for example, would use the struct’s name: case .RGB(r=r, g=0, b=0).
One small benefit of this design change is that Mojo users can reuse their existing knowledge of struct definitions to define enum cases, and to pattern match on them. A case is “just a struct”, so everything you can do with a struct, you can do with a case, more or less. The resultant syntax is also more concise in many situations. Here is some code from Chris’s proposal:
struct SourceRange:
var start: Int
var end: Int
enum Token:
case eof
case source_range(SourceRange)
match some_token:
case .source_range(SourceRange(start=x, end=y)):
...
And here’s what it would look like with case structs:
enum Token:
case EOF
case struct SourceRange:
var start: Int
var end: Int
match some_token:
case .SourceRange(start=x, end=y):
...
The two notable differences are that:
- We were able to define
SourceRangeinside the definition ofToken. - We were able to eliminate the
.source_rangewrapper label in both the enum definition and the pattern match.
Overall, I think this is a nice clean up. I expect the nearest-term benefit of modelling cases as structs will be to help Mojo users remember how to read and write enum definitions, especially enums that have complicated payloads. But more importantly, this design direction opens up a ton of opportunities that we can explore in the future, after this MVP lands. I’ll dive into those next.
Opportunities provided by this design (post-MVP)
I will now explain why modelling cases as structs is actually interesting/useful. There are a lot of opportunities here, and we don’t need to pursue all of them. I will start with features that I think are “easy wins”, and gradually progress to more complex (but powerful) features. You should think of everything below as options enabled by the MVP, rather than a list of features I’d like to see implemented. My intention is to keep the door open to these opportunities, by ensuring that the enum MVP doesn’t preclude them.
Using pattern matching to “downcast” an enum instance
If enum cases are types, then by pattern matching, we should be able to obtain an instance of that type:
enum Tree[T: type]:
@fieldwise_init
case struct Leaf:
var value: T
@fieldwise_init
case struct InteriorNode:
var value: T
var left: Box[Tree[T]]
var right: Box[Tree[T]]
var tree: Tree[Int32]
match tree:
case .Leaf as var x: # x has type `Tree[Int32].Leaf`
...
case .InteriorNode as var x: # x has type `Tree[Int32].InteriorNode`
foo(x.left) # now we can access the fields `.left` and `.right`
Note: The as keyword is part of Python’s pattern matching grammar and is planned for Mojo as well.
What we are doing here is narrowing or refining the matched value, producing a “more specific type” based on the information we have learned. This is a form of subtyping or type refinement. In the short term, Mojo doesn’t need true subtyping to implement this feature, but it’s something to keep in mind as a direction we can go in, to support more advanced use cases.
If we choose our syntax for Mojo’s “if let” feature carefully, we can also do this rebinding within if-statements:
var tree: Tree[Int32]
if tree case .InteriorNode as x:
foo(x.left)
I find this if-let syntax easier to read than the Rust-style syntax if var <pat> as <ident> = tree. And I like the consistency with the syntax of match statements. But that’s just a personal preference.
If Mojo was to support some form of flow-sensitive type narrowing (a la TypeScript), we could make this code even more succinct:
var tree: Tree[Int32]
if tree case .InteriorNode:
foo(tree.left) # in this scope, `tree` now has type `Tree[Int32].InteriorNode`
But this opens up a whole can of worms, so I think for the moment, the first option (binding a new name to narrow a type) is the better one.
Storing and manipulating case structs
Once we have the ability to downcast case structs, we can do all kinds of useful things with them. For example, we can partition a list of enum instances into a list of its cases:
enum Color:
case struct RGB: ...
case struct HSL: ...
var colors: List[Color] = get_colors()
var rgb_colors: List[Color.RGB] = []
var hsl_colors: List[Color.HSL] = []
for color in colors:
match color:
case .RGB as x:
rgb_colors.append(x)
case .HSL as x:
hsl_colors.append(x)
And as Owen mentioned in his last post, we can write useful functions to convert between cases:
def convert_to_hsl(rgb: Color.RGB) -> Color.HSL:
...
This all feels pretty useful. Of course, you can do something similar by defining RGB and HSL as standalone structs that an enum wraps, but this adds boilerplate to both the struct definition and the pattern matching. The mechanics of case structs that I’ve presented so far seem simple enough to be worth implementing, even if just to avoid this boilerplate. But if we’re looking for stronger justification for case structs, continue reading!
Guaranteed compatibility between Pointer[MyEnum] and Pointer[MyEnum.Case]
Earlier I mentioned that a case struct is not “just” a payload type, because it also (implicitly) stores a discriminant, and has the size and alignment of the parent enum. Here is where that distinction comes into play.
Consider the earlier example where we partitioned colors into RGB and HSL. Let’s imagine we want the partitions to store pointers rather than copies of the colors:
var colors: List[Color] = get_colors()
var rgb_colors: List[Pointer[Color.RGB, _]] = []
var hsl_colors: List[Pointer[Color.HSL, _]] = []
Now let’s imagine we’ve defined a function that takes an immutable pointer (or reference) to a color:
def foo(color: Pointer[Color, _]): ...
It would be great if we could safely pass a pointer to an RGB or a HSL color to this function, even though they expect a more general Color pointer. (For a more sophisticated example, imagine passing an arbitrary data structure full of such pointers.) Such a function call is not “safe” unless it’s valid to reinterpret an RGB pointer as a Color pointer. This is where it becomes important that each case struct inherits the discriminant and alignment of its parent enum. If we can guarantee this, then (as long as the caller doesn’t mutate the pointee), an RGB pointer is just a Color pointer where you know the value of the discriminant. The callee can match on that discriminant, rediscovering the information the caller already knew. The RGB pointer is guaranteed to be aligned correctly. Everything plugs together just fine.
In practice, to convert between pointers we would either need to bitcast them:
var p: Pointer[Color.RGB, _]
foo(p.unsafe_bitcast[Color]())
…or Mojo’s type system would need to support subtyping, and the associated type-compatibility rules. Thankfully, it seems like such a feature is already (whether intentionally or not) on the Mojo roadmap, given that Python-style classes (with inheritance) naturally introduce subtyping to any static type system.
There are also some subtleties when it comes to mutability, but that’s worth a post of its own, so for the moment, I’ll move onto other topics. If you know anything about the relationship between mutability and variance in languages with subtyping, that’s the issue at play here.
In summary: we have the opportunity to offer guaranteed “compatibility” between (pointers to) case structs and their enclosing enum type. But all of this requires case structs to carry discriminants and inherit the enum’s alignment, so case structs cannot be replaced by Swift/Rust-style “tagless” payload values. They are a fundamentally different concept, that requires proper treatment at the language level.
Reminder: I’m just discussing opportunities for Mojo’s evolution. None of this needs to be part of the enum MVP! It’s just a door that we can keep open if we design the MVP carefully.
Case unions
I want to talk about one more opportunity that Mojo could pursue some day: case unions.
Python already has a concept of class unions:
x: int | str
The principle that makes class unions work in Python is that of uniform representation: every Python reference is encoded as a pointer to its payload, stored alongside (effectively) a discriminant, that can be used to check whether x is an int or a str at runtime. A Python “union” is therefore exclusively a compile-time concept: it specifies what values the reference can take on at runtime, without influencing the runtime representation of the value.
In Python we can match on x to discover its type:
match x:
case int(): ...
case str(): ...
This is very reminiscent of our enum pattern matching syntax. That raises the question: can Mojo offer language features that facilitate both static typing of classes, and static typing of enums?
I think there’s a lot of promise here. What if we can offer the concept of (enum) case unions, to mirror that of Python class unions?
Here’s how we might declare a color variable whose case is restricted to be an RGB or HSL color:
var c: Color.RGB | Color.HSL
Just as in Python, this “union operator” would not introduce a new class or struct. The type of c would be a subtype of Color, and its runtime representation would be identical to a Color. As in Python, we can then rely on the presence of the discriminant to test which (sub)type the variable holds at runtime:
match c:
case Color.RGB: ...
case Color.HSL: ...
This match would be considered exhaustive, even if the Color enum contains other variants, because the type of c indicates that c does not hold those remaining variants.
It’s important to clarify: this “union operator” is not the same concept as “anonymous sum types”. If that were the case then the type of c would be a brand new type, distinct from the Color enum and its cases, and it would introduce an additional discriminant value to c that makes its runtime representation different to that of a Color. Instead, we are retaining the representation of Color, and providing some extra compile-time knowledge about which colors c will store.
This also implies that it would be nonsense to take the union of two (non-case) structs, e.g. Pointer[X] | Pointer[Y], because such structs do not store a discriminant that can be used to differentiate them at runtime.
This also implies that Color.RGB | Color.RGB should be the same type as Color.RGB. Indeed, this is why “class unions” (and my proposed “case unions”) use the word “union”: the semantics reflects that of a set union.
In summary: by modelling Mojo’s enums as a set of “case structs” we are opening a pathway towards (some day) supporting “case unions” that behave exactly like Python “class unions”. In fact, I would go as far as to say that the two language features can actually be merged into one, with the notion of a “common discriminant” (across the unioned cases) being the unifying principle. This would help to make Mojo’s type system expressive, offering the typing features that Python users expect while keeping the language small.
Conclusion
I hope the intention behind this post makes sense to everyone. To recap: there’s a certain model for enums (that of case structs) that opens up a lot of exciting possibilities for Mojo’s future evolution. We don’t have to pursue all of those possibilities, but it would be good to leave them on the table.
It would be difficult to implement the possibilities I’ve presented above if we were to stabilize Chris’s original proposal as-is, so we really do need to do some advance planning if we would like to keep these doors open. (Or else we’d need to be willing to do a major “enums 2.0” redesign that amongst other things, changes the enum syntax.)
Another benefit of the case struct proposal is that it’s more concise than declaring “out-of-line” payload structs (as discussed early in this post), so that’s an added win. The main downside I can see with my alternative MVP is that case struct: ... is more verbose than case(X, Y, Z). In my opinion this is not a big deal, given the tradeoff we’d be making.
It seems Owen and I have converged on the same core design, for what that’s worth as well. (I wasn’t aware of this until I saw Owen’s post just now.)
That’s all I have to say for now. Please let me know what you all think. Thanks for reading!
As you noted, the main advantage of the “tuple-case” syntax for enum is that the code becomes more concise when payload is a struct from an external library.
enum Shape:
case Circle(Float64)
case Square(Float64)
case Rectangle(Float64, Float64)
enum Shape:
case struct Circle:
var value: Float64
case struct Square:
var value: Float64
case struct Rectangle:
var hight: Float64
var width: Float64
Thus, none of the variants is ideal, but their drawbacks are acceptable if we have to choose only one of them.
I think it’s worthwhile to keep the tuple syntax around, since it’s already implemented and for “trivial” enums it does make it much more concise.
I don’t think this is a good idea, because it messes with niche optimizations unless you want to define it as always exactly matching, and I don’t think it’s worthwhile to carry around the discriminant when you have just “lifted” that to comptime with the match statement.
Other than that, I think I generally agree with Nick here.
var c = Color.RGB(100, 240, 70) # c has type
Color, notColor.RGB
One concern I have is that the constructor returns a different type from itself and Mojo has no inheritance of structs and this I think will cause issues in future if we ever introduce inheritance and that would constraint the future design to consider this special case.
What if the enum declaration defines a trait instead of struct. Then each of the case is implementing the enum trait. This will keep type conformance, while maintaining the restriction that enum cannot have runtime state.
P.S. enum would be “private” trait and only implemented by its cases.