On Mon, Dec 20, 2021 at 4:04 AM Leonard Mittmann
<
leonard....@gmail.com> wrote:
>>
>> As to _why_ this is the case, the generics proposal has a section about that:
>>
https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#permitting-constraints-as-ordinary-interface-types
>
> @Jason Phillips Thanks for pointing me here.
>
> @Ian Lance Taylor I agree with you that both "old"-interfaces and type-interfaces have the same semantics. However, syntactically they are different and this is the reason for my discomfort.
>
> It makes total sense that my code sample won't work even if type-interfaces could be used for variables in the future as the type could not be determined at compile time. But this is very different from the way "old"-interfaces work. In a slice of io.Reader's I can instantiate various types that implement the interface and pass them on to some func consuming an io.Reader.
>
> Maybe this is only me not seeing the bigger picture and possible benefits of reusing interfaces for type constraints. I am curious if there are other cases than my described scenario where this might make total sense. For now I remain skeptical though and find it very confusing trying to fit two syntactically different things in the same term "interface".
There was years of work getting to this point. For a while we had
constraints (then called contracts) that were distinct from interfaces
(you can see this at
https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-contracts.md).
At that stage we got a lot of feedback of the form "Constraints are an
awful lot like interfaces (see
https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-contracts.md#why-not-use-interfaces-instead-of-contracts).
Why are they distinct? How do we know which one to use?" Changing
constraints to actually be interface types was a significant
simplifying step. We reduced two similar concepts to one.
I want to stress that what you describe as "two syntactically
different things" are only one thing. Interface types used as
constraints are strict supersets of interface types used as variable
types.
Further, It is possible that in the future we will permit all
interface types to be used as variable types. We didn't do that
immediately because we didn't want additions for generics to change
the existing non-generic language. If we do indeed implement that,
then there will clearly be no distinction between the various sorts of
interface types.
Ian