On Mon, Nov 27, 2017 at 2:42 PM, Stefan Nilsson <
trolleriprofess...@gmail.com> wrote:

> I see the dynamic type, which is part of every interface variable, as more
> than an implementation detail.
>

Yes. But not the type descriptor. Which is why I tried to emphasize the
difference :)

The dynamic type is a semantic concept, not related to any specific
implementation. Every implementation needs to implement interfaces some way
and that way needs to support the notion of a dynamic type.
The type descriptor, is what I'd call this thing
<https://github.com/golang/go/blob/master/src/runtime/type.go#L28> (and the
other structs in that file, embedding it): A data structure defined by the
runtime and generated by gc for every type used in the program. It's the
way how gc - this particular implementation of Go - chose to implement the
requirement of dynamic types from the spec.

The fact that interface values have dynamic types and dynamic values is not
an implementation detail, it's required by the spec.
The fact that interface values are represented by a two-pointer struct, one
pointer to the dynamic value and one pointer to a (unique) type descriptor
*is* an implementation detail. A Go implementation might choose to inline a
type-definition, or to use integer-id's instead of singleton-pointers, or
to inline dynamic values, or to not box interfaces at all if it can proof
it unnecessary, or to box *every* value, not just interfaces, or…

(Note, this is still just the nomenclature I use. Just trying to explain
why it's self-consistent and IMO reasonably clear)


> The language spec, which is typically very careful to avoid talking about
> implementation issues, explicitly states that an interface variable has a
> dynamic type:
>
> Variables of interface type also have a distinct *dynamic type*, which is
> the concrete type of the value assigned to the variable at run time (unless
> the value is the predeclared identifier nil, which has no type). The
> dynamic type may vary during execution but values stored in interface
> variables are always assignable
> <https://golang.org/ref/spec#Assignability> to the static type of the
> variable.
>
> The dynamic type is also crucial in the definition of equality, and it's
> the very mechanism used in type assertions and type switches.
>
> Thinking of an interface variable as storing both a concrete value and a
> dynamic type can be really helpful for understanding Go interfaces. First,
> it's correct by definition. Second, trying to apply concepts from different
> languages can lead to fuzzy thinking. In my experience, the Go language
> spec is often to the better teacher. :)
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to