Skip to content

Instantly share code, notes, and snippets.

View ryanwarfield's full-sized avatar
🇨🇦
:D

Ryan Warfield ryanwarfield

🇨🇦
:D
View GitHub Profile
@ryanwarfield
ryanwarfield / typed_unions.md
Created April 10, 2026 20:42
Why typed unions are the best thing ever.

Typed Unions!

Without typed unions, its hard to make illegal states unrepresentable. For example, if we want to keep track of a Translink bus, you might want to restrict the state. If it is moving, you want the speed and the direction. If its stopped, you want to keep track of if the door is open. In this case, the practical way of doing this in go (without a billion lines of interfaces) is:

type BusState struct {
    isMoving    bool
 speed int32