Union Types and Option Types
tibleiz.net·41w
Preview
Report Post

2025-03-16

In past years, I’ve experimented union types in a low level language (Copper) and a higher level language. I got very interesting results but I’m currently experimenting a trimmed down version: just the option types.

What is an Union Type?

An union type is a list of two or more alternative types that a value can hold. For example:

func f(x: Int | String)
...
end

the parameter x can be either a string or an integer.

Some languages accept to perform directly operations that are commons to all types of the union, for instance if Int and String types have a print method, you could just do x.print. But in the basic case, you must first discriminate the actual type of the value before using it:

func f(x: Int | String)
match x
case Int -> i
// ...

Similar Posts

Loading similar posts...