by mamcx · 5y ago · 0 comments
enum Value {
Int32(i32),
Int64(i32),
Str(String),
}
fn only_int(of:Value[Int32, Int64]) -> Value.Str
And other times: enum ExtraValue:Value {
Bool(bool),
}
This is analog to structs, where eventually is "discovered" that is nice to: let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
active: user1.active,
sign_in_count: user1.sign_in_count,
};
let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
..user1
};
But somehow enums are still considered second-class in the algebraic type space. type Foo = { kind: 'A', aItem: number } | { kind: 'B', bItem: string } | { kind: 'C' };
type SubsetFoo = Foo & { kind: 'B' | 'C' }
type SupersetFoo = Foo | { kind: 'D', dItem: boolean };
I'm sure there are imperfections here. For a start, SubsetFoo's normalized form looks rather ugly when you mouse over it in VSCode. But it does get you the niceties of exhaustiveness checking and type-aware suggestions with control flow awareness, etc.. type SubsetFoo = Extract<Foo, { kind: 'B' | 'C' }>;In your example, Scala 2 would let Int32 and Int64 inherit a trait that Str doesn't. TypeScript would let you write a union type Int32 | Int64 that doesn't include Str (or add ExtraValue), and I think Scala 3 will have something very similar.
You can do
data Value a =
I32 Int32 a
B Bool
and then type Value1 = Value ()
type Value2 = Value Void
as a hack