Repository: go
Issue: spec: define "interface value"
Labels: Documentation,NeedsInvestigation

by **gnormington@gopivotal.com**:

<pre>The spec mentions &quot;interface value&quot; seven times but does not define the term.
Defining the term carefully would help to avoid some common misunderstandings, most
notably <a href="http://golang.org/doc/faq#nil_error">http://golang.org/doc/faq#nil_error</a>.

The spec says &quot;Two interface values are equal if they have identical dynamic types
and equal dynamic values or if both have value nil.&quot; from which we can infer:

1. An interface value may have a dynamic type, and

2. An interface value may have a dynamic value, and either

3a. An interface value may have value nil, or

3b. An interface value may be nil.

I think the spec sentence should really say &quot;Two interface values are equal if
[...] or if both are nil.&quot; which would imply 3b rather than 3a.

The spec also says &quot;The static type (or just type) of a variable is the type
defined by its declaration. Variables of interface type also have a distinct dynamic
type, which is the actual type of the value stored in the variable at run time. The
dynamic type may vary during execution but is always assignable to the static type of
the interface variable. For non-interface types, the dynamic type is always the static
type.&quot; from which we can additionally infer (assuming a variable of interface type
always contains an interface value):

4. An interface value has a static type which is the interface type of the value.

Notice that 4 doesn't appear to be true for nil interface values, so I think the above
paragraph needs fixing too (&quot;[...] Non-nil variables of interface type also have a
distinct dynamic type, [...]&quot;)

Although I suspect the FAQ isn't normative, it provides a helpful insight, &quot;[...]
the return value is an error interface value holding (*MyError, nil)&quot;, from which
we can infer:

5. The interface value may have a nil dynamic value.

Here is a speculative description of an interface value, with some educated guesses
thrown in, to give an impression of where all this might lead:

An interface value is the value of a variable of interface type. An interface value is
either nil or has both a dynamic type and a dynamic value.

A nil interface value always results from [is assigned from, passed as a parameter,
returned, etc.] a literal nil value since any other value has a non-nil dynamic type.

The static type of an interface value is an interface type [and it would be nice to
spell out *which* interface type!].

If a non-nil interface value has a non-nil dynamic value, then the actual type of the
dynamic value is equal to the dynamic type of the interface value. The dynamic value of
a non-nil interface value may be nil.

The dynamic type of a non-nil interface value is never nil. The dynamic type of a
non-nil interface value implements the static type of the interface value.

An interface value compares (==) equal to nil if and only if it is nil; a non-nil
interface value never compares (==) equal to nil. [This latter statement may be out of
place in the definition of interface value, but should at least be linked to from the
definition. But given <a href="http://golang.org/doc/faq#nil_error">http://golang.org/doc/faq#nil_error</a>, it may be worth having some
repetition of this point.]</pre>