Hacker News new | ask | show | jobs
by nicoburns 2294 days ago
I guess it's kind close to a C++ class. It's pretty different to a class in a language like Java, because all classes in Java are heap allocated and behind references.

Enums are are the better example of non-class types. For example, you can have:

    enum StringOrInt {
        String(String),
        Int(u32),
    }
And you can go ahead and implement methods on that type. Classes have "AND-state", not "OR-state". But a Type in general can have either kind of state.
1 comments

That's equivalent to wrapping an enum in a class. Emulations of type hierarchies without OO often fail like this, having a A-or-B be literally the same type so losing out on type safety/forcing constant rechecking of the discriminant.
That can be done in Rust by having two different types implement the same interface