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
go/types: Unclear what typ param in NewTypeName is #22516
Comments
First of all, we don't use the issue tracker for questions. Also, please, refrain from shouting questions by ending them in '???????...'; the design is not as insane as the '?'s imply... Thanks. Regarding the documentation: NewTypeName creates a new TypeName as the signature implies. That object, TypeName, represents a name for a named or alias type. That's in fact documented with TypeName, 5 lines above. Thus, the typ argument may or may not be the underlying type, and hence it's not called underlying, exactly because that would be confusing. NewNamed creates a new NamedType, where NamedType is the historical name we used for what is now called a defined type in the spec. For backward-compatibility we couldn't change those names. A named (or defined) type has an underlying type, which is the type on which the new named type is based. A NamedType points to its TypeName which points back to its NamedType. If you read the 8 lines of code in NewNamed you will see how that connection is established. The go/types API is not really documented for general consumption (it would fill reams of pages); it does require a bit of digging and reading the code. |
I’m sorry the question marks came across as shouting. It was meant to be a placeholder that would stand out from the surrounding code, since I referred to it in the paragraph below. I’ll use normal variable names instead in the future.
This is about a documentation issue, not a documentation question. The question is asked to demonstrate the issue. If I’d omitted the question, I think you’d be left scratching your head, trying to guess why it’s unclear to me. How should I handle documentation that seems unclear in the future? Assuming I’ve already asked others, that is.
The param name explanation makes sense.
If Named and TypeName refer to each other, then how does one use their New constructors to set that up? You can’t call NewNamed without a TypeName, and you can’t call NewTypeName without already having the Named, if I understand correctly.
If go/types isn’t meant for general consumption, then I don’t understand why it’s in the standard library. I thought it was partly meant to aid writing Go tools?
… On Oct 31, 2017, at 2:44 PM, Robert Griesemer ***@***.***> wrote:
First of all, we don't use the issue tracker for questions. Also, please, refrain from shouting questions by ending them in '???????...'; the design is not as insane as the '?'s imply... Thanks.
Regarding the documentation:
NewTypeName creates a new TypeName as the signature implies. That object, TypeName, represents a name for a named or alias type. That's in fact documented with TypeName, 5 lines above. Thus, the typ argument may or may not be the underlying type, and hence it's not called underlying, exactly because that would be confusing.
NewNamed creates a new NamedType, where NamedType is the historical name we used for what is now called a defined type in the spec. For backward-compatibility we couldn't change those names. A named (or defined) type has an underlying type, which is the type on which the new named type is based.
A NamedType points to its TypeName which points back to its NamedType. If you read the 8 lines of code in NewNamed you will see how that connection is established.
The go/types API is not really documented for general consumption (it would fill reams of pages); it does require a bit of digging and reading the code.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I see what you're saying. The many ?'s threw me off; I didn't see that they meant to be a place holder; sorry about that. I understood it's a documentation issue, and so I left it open because of that. (Just saying that it's unclear from documentation what/which type you need to provide to NewTypeName would probably be enough; especially because that method actually is missing a doc string. It would have been pretty clear.) But to your question: If you look at NewNamed, as I have alluded to in my prior comment, you will see that NewNamed actually connects back from the provided TypeName to the Named type, if necessary. So you can call NewTypeName with a nil type and then provide it to NewNamed which will then connect them. Btw., I didn't say go/types isn't meant for general consumption; I said it's not documented for general consumption. The primary use case for go/types is to produce type information for a given package, which then can be queried. The primary reason why methods and functions for constructing go/types data structures are exposed is so that we can write importers (which are in different packages). When we wrote it we (Alan Donovan and I) agonized how to get around this, well knowing that it would lead to endless troubles down the road exactly because proper use of these constructors requires intimate knowledge of how type graphs are constructed; something that we didn't meant to expose for general consumption. Hence the missing documentation. |
Change https://golang.org/cl/75595 mentions this issue: |
For the record: I'm glad that these functions are exported and am using them to implement a custom, database-backed importer. |
Makes sense.
Makes sense. Sorry, I was on my phone and didn't have the source in front of me to check out.
My mistake. I see what you mean, but in my mind, if a package is meant for general consumption, it should be documented as such.
If the constructors aren't meant to be used, it would be clearer if their documentation said so. Or perhaps it could say they're deprecated. Leaving them out there and not documented can be frustrating to newcomers who don't know. There don't seem to be that many complications, though. Would the additional documentation be that much more? This documentation issue, for example, could be explained with two additional lines in the NewTypeName and NewNamed documentation: // NewTypeName [...]. If the type name will be used to make a named type, the type
// should be nil, and NewNamed will set it.
func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName
// NewNamed [...]. It sets the type name's type to the named type.
func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named ...and I just saw the associated changelist! Thank you. :) |
@dominikh Agreed. |
https://golang.org/pkg/go/types/#NewTypeName:
Is it the underlying type? If so, that's confusing, because the underlying type param for NewNamed is named "underlying" and this one is not.
https://golang.org/pkg/go/types/#NewNamed:
To construct a new named type, it's confusing why you have to supply a types.Type twice, especially if the typ param of NewTypeName isn't the underlying type:
What should the argument be for the typ param of the NewTypeName call above (where
??????????
is)? Why does TypeName need it?Does this issue reproduce with the latest release (go1.9.2)?
Yes
System details
The text was updated successfully, but these errors were encountered: