top | item 40218785

(no title)

throwaway143829 | 1 year ago

Enums and sum types seem to be related. In the code you wrote, you could alternatively express the Hot and Cold types as enum values. I would say that enums are a subset of sum types but I don't know if that's quite right. I guess maybe if you view each enum value as having its own distinct type (maybe a subtype of the enum type), then you could say the enum is the sum type of the enum value types?

discuss

order

randomdata|1 year ago

> Enums and sum types seem to be related.

They can certainly help solve some of the same problems. Does that make them related? I don't know.

By definition, an enumeration is something that counts one-by-one. In other words, as is used in programming languages, a construct that numbers a set of named constants. Indeed you can solve the problem using that:

    type Temperature int

    const (
        Hot Temperature = iota
        Cold
    )

    func SetThermostat(temperature Temperature) {
        switch temperature {
        case Hot:
            fmt.Println("Hot")
        case Cold:
            fmt.Println("Cold")
        }
    }
But, while a handy convenience (especially if the set is large!), you don't even need enums. You can number the constants by hand to the exact same effect:

    type Temperature int

    const (
        Hot  Temperature = 0
        Cold Temperature = 1 
    )

    func SetThermostat(temperature Temperature) {
        switch temperature {
        case Hot:
            fmt.Println("Hot")
        case Cold:
            fmt.Println("Cold")
        }
    }
I'm not sure that exhibits any sum type properties. I guess you could see the value as being a tag, but there is no union.

lelanthran|1 year ago

Unfortunately, this:

    const (
        Hot  Temperature = 0
        Cold Temperature = 1 
    )
Isn't really a good workaround when lacking an enumeration type. The compiler can't complain when you use a value that isn't in the list of enumerations. The compiler can't warn you when your switch statement doesn't handle one of the cases.

Refactoring is harder - when you add a new value to the enum, you can't easily find all those places that may require logic changes to handle the new value.

Enums are a big thing I miss when writing Go, compared to when writing C.

nyssos|1 year ago

Enums are exactly sums of unit types (types with only one value).

lsaferite|1 year ago

Regardless of the rest of this thread, I appreciate this comment. It helped crystalize 'enum' in the context of 'sum' for me in a way that had previously been lacking. Thanks.

randomdata|1 year ago

Traditionally, enums have been a single number type with many values (initialized in a counted one-by-one fashion).

Rust enums are as you describe, as they accidentally renamed what was historically known as sum types to enums. To be fair, Swift did the same thing, but later acknowledged the mistake. The Rust community doubled down on the mistake for some reason, now gaslighting anyone who tries to use enum in the traditional sense.

At the end of the day it is all just 1s and 0s. If you squint hard enough, all programming features end up looking the same. They are similar in that respect, but that's about where the similarities end.