-
Notifications
You must be signed in to change notification settings - Fork 18k
spec: define "interface value" #8150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Documentation
Issues describing a change to documentation.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
Comments
An interface value is an instance of an interface type[0] as is an integer value an instance of an integer type. An interface has always both a static type and a dynamic type. An interface value (IV) can be nil and can hold a nil value. The former situation applies when the IV has no dynamic type because nothing has been assigned to the IV. The later situation applies when the dynamic type of the IV is nil. The static type of an IV is the type IV was declared to be: var v1 interface{} var v2 myInterface var v3 int The static type doesn't change exactly as the type of v3 never changes. Etc., ie. all (of the other) observations in the OP are already well defined in the language specification. [0]: http://golang.org/ref/spec#Interface_types |
The term interface value is misleading as it implies that there's a value of type interface, but in fact there are no values of such types because interfaces types are abstract types, with no concrete instances. Owner changed to @griesemer. Status changed to Accepted. |
Comment 4 by gnormington@gopivotal.com: Thanks for the fast feedback. > An interface value is an instance of an interface type[0] > as is an integer value an instance of an integer type. Of course, but that just pushes the problem back to what it means to instantiate an interface type, which I don't think is clear in the spec. > An interface has always both a static type and a dynamic type. I find this really confusing. The spec defines "interface" as follows: "An interface type specifies a method set called its interface." How can a method set have a dynamic type? Surely only values have dynamic types? > An interface value (IV) can be nil and can hold a nil value. Perhaps "can hold a nil dynamic value" would be less confusing, if that's what you mean. > The former situation applies when the IV has no dynamic > type because nothing has been assigned to the IV. The later > situation applies when the dynamic type of the IV is nil. You seem to be saying that: var v1 myInterface results in v1 being nil and having no dynamic type whereas var v1 myInterface v1 = nil results in v1 holding a nil value and having a nil dynamic type. Agreed? But there is another case to consider: an IV which has a non-nil dynamic type and a nil dynamic value, e.g. (*MyError, nil) from the FAQ. > The static type of an IV is the type IV was declared to be: > > var v1 interface{} > var v2 myInterface > var v3 int > > The static type doesn't change exactly as the type of v3 never changes. Sure. > > Etc., ie. all (of the other) observations in the OP are already > well defined in the language specification. If by OP, you mean the opening text of this bug report, I'm afraid I disagree. With a bit of polishing up (or even correction - see above), the following statements would be a valuable addition to the spec: > An interface value (IV) can be nil and can hold a nil value. > The former situation applies when the IV has no dynamic > type because nothing has been assigned to the IV. The later > situation applies when the dynamic type of the IV is nil. but, if these are already well defined in the spec, I'm wondering where as I've clearly not found the right section yet. |
Comment 5 by gnormington@gopivotal.com: > The term interface value is misleading as it implies > that there's a value of type interface, but in fact > there are no values of such types because interfaces > types are abstract types, with no concrete instances. This I understand. Thank you! However, that seems to leave the FAQ high and dry. Doesn't the spec needs to say something from which the FAQ could be inferred? |
A variable of (static) interface type can hold a value of concrete (non-interface) type; that type is also called the "dynamic type" of that variable (since it can change dynamically during the run of a program). Each such value's concrete type (== the variable's dynamic type) "implements" the interface type of the variable. The variable value may also be nil, and in this case has no dynamic type because there's no concrete value in the variable (and I'm casually claiming that "nil" is not a concrete value). A value can never have interface type. But let's not discuss this in this CL. There are already several issues open that ask for the clarification of interface types, so eventually we will get to them. The CLs are the place for the refinement. This is also not urgent since all implementations agree on what is meant (even if it's not written down clearly); and generally programmers seem to understand the intent. |
Comment 7 by gnormington@gopivotal.com: Ok, I'll look forward to a more precise description of interface types and variables of interface types. I'm happy to review any proposal and provide early feedback - just post a link here if I can be of service. Although I agree that programmers seem to be getting by with the current level of explanation of interface types, I don't feel there has yet been a sufficiently compelling and precise explanation and, when there is, I think all Go programmers will be considerably better off. |
CL https://golang.org/cl/149360043 mentions this issue. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Documentation
Issues describing a change to documentation.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
by gnormington@gopivotal.com:
The text was updated successfully, but these errors were encountered: