# Proposal: unify trait composition and std.Variant

**URL:** <https://forum.modular.com/t/proposal-unify-trait-composition-and-std-variant/2876>\
**Category:** Mojo\
**Created:** [March 22, 2026, 4:43pm UTC](https://forum.modular.com/t/proposal-unify-trait-composition-and-std-variant/2876 "2026-03-22T16:43:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Turcik](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@Turcik](https://forum.modular.com/u/Turcik)\
**Post date:** [March 22, 2026, 4:43pm UTC](https://forum.modular.com/t/proposal-unify-trait-composition-and-std-variant/2876/1 "2026-03-22T16:43:48Z")

</div>

Currently, trait composition is done using `&`, e.g. (from the docs): `comptime DuckLike = Quackable & Flyable`

At the same time, struct “composition” is done using `std.Variant`, e.g. `comptime IntOrString = Variant[Int, String]`. Notably, there is no shorthand such as `comptime IntOrString = Int | String`

Python is using `Union` and the shorthand `|` for this purpose. Interestingly, in the Mojo docs, the term **union** is used to describe `Variant`.

> `Variant` - A **union** that can hold a runtime-variant value from a set of predefined types.
> 
> `Variant` is a discriminated **union** type

Unifying these (and aligning with Python) would make it easier to teach and use. Maybe in the future these would be valid (e.g. via dynamic dispatch or by synthetizing the union):

`def foo(flying_quacker: Quackable | Flyable)` or `def foo(flying_quacker: Quackable & Flyable)`  
but also  
`def bar(int_or_str: Int | String)`

compare with the current:  
`def foo[T: Quackable & Flyable](flying_quacker: T)`  
and  
`def bar(int_or_str: Variant[Int, String])`

Or at the very least, keeping trait composition as is and renaming `Variant` to `Union` and allowing the shorthand `|` would be a good change in my opinion.

---

<div class="post-metadata">

**Author:** ![owenhilyard](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/owenhilyard/32/41_2.png) [@owenhilyard](https://forum.modular.com/u/owenhilyard)\
**Post date:** [March 22, 2026, 9:59pm UTC](https://forum.modular.com/t/proposal-unify-trait-composition-and-std-variant/2876/2 "2026-03-22T21:59:25Z")

</div>

We are saving the `Int | String` syntax for a proper anonymous tagged union type that can support pattern matching, since there is general agreement that something more like Rust’s enum is preferable to `Variant`. The reason that trait unions are treated separately is because they can’t exist on their own, they’re a way to name a trait which depends on all of the traits that make up the union.

`Union` is a name quasi-reserved for unsafe, C-like unions and variants of those (ex: using linear types and typestate to manage the active variant), which we need to have in Mojo to be able to do C interop, so I consider that name taken.

---

<div class="post-metadata">

**Author:** ![Turcik](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@Turcik](https://forum.modular.com/u/Turcik)\
**Post date:** [March 23, 2026, 9:04am UTC](https://forum.modular.com/t/proposal-unify-trait-composition-and-std-variant/2876/3 "2026-03-23T09:04:00Z")

</div>

Hi, thanks for the reply! Shouldn’t the default be whatever the Python community is used to? For the C interop Mojo could very well use `CUnion` or `UnsafeUnion` or `NativeUnion`, or potentially just `std.ffi.Union` (which would be different from `std.utils.Union`)

The most common use-case will be reproducing Python functionality (i.e. rewrite Python code in Mojo), C interop is a corner case for which familiarity and simplicity is less valuable.

---

<div class="post-metadata">

**Author:** ![owenhilyard](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/owenhilyard/32/41_2.png) [@owenhilyard](https://forum.modular.com/u/owenhilyard)\
**Post date:** [March 23, 2026, 1:29pm UTC](https://forum.modular.com/t/proposal-unify-trait-composition-and-std-variant/2876/4 "2026-03-23T13:29:25Z")

</div>

There was some discussion about that, but it tended to cause a lot of confusion both among systems programmers (who assumed that `Union` was a tagged union instead of a type union), and python programmers (“What are all of these extra kinds of Union?”). It would also result in a lot of python programmers accidentally using the extremely unsafe constructs and then getting confusing crashes.

While I’m sympathetic to the break with python here, my python programmers never even see the `Union` type name, instead using `str | int`, and placing that up against decades of expectations from another major group of Mojo programmers means I don’t think it’s worth the confusion.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [September 19, 2026, 1:29pm UTC](https://forum.modular.com/t/proposal-unify-trait-composition-and-std-variant/2876/5 "2026-09-19T13:29:42Z")

</div>

Closed on September 19, 2026, 1:29pm UTC

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