Skip to content
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

spec: extend `Type assertions` section #38180

Open
fabriziocucci opened this issue Mar 31, 2020 · 4 comments
Open

spec: extend `Type assertions` section #38180

fabriziocucci opened this issue Mar 31, 2020 · 4 comments
Assignees
Milestone

Comments

@fabriziocucci
Copy link

@fabriziocucci fabriziocucci commented Mar 31, 2020

Today I stubled upon this issue and I was hoping to find, in the reference doc, more examples of type assertions on pointer types but, unfortunately, there seems to be none.

Would it make sense to extend the current Type assertions section with this?

(BTW I'm a Go newbie so apologies in advance if I missed something in the existing doc!)

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 31, 2020

I'm not sure quite what you are suggesting. The blog post looks right, but it's not saying anything that the language spec doesn't already say, albeit tersely. The language spec already says that the type must be identical. Do you think we should just add a couple more examples?

@ianlancetaylor ianlancetaylor changed the title [doc][enhancement] Extend `Type assertions` section in the doc spec: extend `Type assertions` section Mar 31, 2020
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Mar 31, 2020
@fabriziocucci
Copy link
Author

@fabriziocucci fabriziocucci commented Apr 1, 2020

@ianlancetaylor thanks for your prompt reply! :)

The blog post looks right, but it's not saying anything that the language spec doesn't already say, albeit tersely. The language spec already says that the type must be identical.

I agree, the information is there but, as you said, it is probably packed in a way not easily understandable, especially for Go newbie like me (I'm also coming from a Java background and that doesn't help for sure). As additional context, before being bitten by the mentioned issue (i.e. impossible type assertion), I did read the type assertions section before but TBH I didn't realise at all that type assertions on pointer types were even a thing (again, probably it's just me).

Do you think we should just add a couple more examples?

My suggestion would be to add at least one example of type assertion involving a pointer type and, if possible, put in words the subtle difference between pointer vs non-pointer type assertion.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 1, 2020

To be clear, I don't think there is any difference between pointer and non-pointer type assertion. An interface value holds a value of some type. That type can be a pointer type or a non-pointer type. Type assertions work exactly the same way.

But I think it would be reasonable to add an example to the language spec of a type assertion to a pointer type. Leaving for @griesemer to decide.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Apr 2, 2020

What @ianlancetaylor said. I think the blog is at least confusing when it says "keep in mind that a pointer to a type is not the same as the type itself". It seems to imply that this is unusual behavior, which it is not.

I suspect the confusion comes from not being intimately familiar with the notion of a pointer type, which is understandable when coming from "pointer-less" types such as Java or JavaScript (never mind that these languages use pointers everywhere under the covers but try to hide that, which leads to other confusions).

I've reread the spec section on type assertions and it seems pretty clear. It talks in general terms about a type T and refers to the notion of type identity. Thus, to understand this one needs to understand (read) the section on types and type identity. The latter section makes it clear that a pointer type *T and its base type T are different. So adding more prose seems not justified here; in general the spec is written pretty concisely and we avoid restating things that are being said elsewhere.

Leaving open in case somebody wants to suggest a good pointer example. Send any such CL to me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.