Sum types in product types

I was just thinking about something like this after seeing this post regarding context in errors. Although I had the inverted idea of adding variant independent fields to enums.

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.

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.

2 Likes