# Sum types in product types

**URL:** <https://internals.rust-lang.org/t/sum-types-in-product-types/20269>\
**Category:** language design\
**Created:** [February 6, 2024, 1:14pm UTC](https://internals.rust-lang.org/t/sum-types-in-product-types/20269 "2024-02-06T13:14:43Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Jon-Davis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jon-davis/32/5453_2.png) [@Jon-Davis](https://internals.rust-lang.org/u/Jon-Davis)\
**Post date:** [February 6, 2024, 6:35pm UTC](https://internals.rust-lang.org/t/sum-types-in-product-types/20269/5 "2024-02-06T18:35:14Z")

</div>

I was just thinking about something like this after seeing this [post](https://internals.rust-lang.org/t/helper-for-passing-extra-context-to-errors/20259) regarding context in errors. Although I had the inverted idea of adding variant independent fields to enums.

```rust
enum MyEnum {
    y: f64; // present for all variants

    V1,
    V2,
}

```

The idea for this was to add a context to enum style errors without needing to duplicate the field into every variant.

```rust
enum Error {
   context: Option<String>;
   backtrace: Option<String>;

   Io(io::Error),
   Sql(sql::Error),
}

```

I know this isn't exactly a sum type in a product type. It's adding fields to enums rather than adding variants to structs, but maybe it can serve as an example use case.

I just don't know how useful these constructs would be in practice. I don't run into these situations often enough that I feel saving a few keys by not having to write a separate type is necessary.

---

_[View the full topic](https://internals.rust-lang.org/t/sum-types-in-product-types/20269)._
