7 votes

Does Go have subtyping?

2 comments

  1. skybrian
    Link
    It's odd, I was thinking "of course Go has subtyping." In particular, this is how interfaces can be thought of as working. But since it's top-level only, instead it can be thought of as doing...

    It's odd, I was thinking "of course Go has subtyping." In particular, this is how interfaces can be thought of as working. But since it's top-level only, instead it can be thought of as doing conversions when assigning variables.

    In Go, variables have a type and values have an underlying type, and when you do an assignment, the top-level type may change while the underlying type doesn't.

    We could think of each variable as having a hidden adapter from the variable's type to the underlying type, and when there's an assignment from one variable to another, a conversion might be done, or a new adapter might be automatically constructed that refers to the same underlying value.

    The rules aren't about what set of types a value satisfies, but rather about what conversions may be done.

    But we could still talk about what interfaces a value with a particular method set satisfies, and that's basically a subtype relationship.

    3 votes
  2. rkcr
    Link
    An interesting dive into the Go type system with an in-depth explanation for why it is so restrictive with its type system.

    An interesting dive into the Go type system with an in-depth explanation for why it is so restrictive with its type system.

    1 vote