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
cmd/compile: should auto dereference <pointer-to-map>[index] #44211
Comments
What is the type of |
type Balance map[Currency]float32
It doesn't work, true. But why not? Doesn't the compiler have enough information? |
The compiler only implicitly dereferences on pointers to arrays, not pointers to maps (or other types, like slices). So if you had
it would work. I don't think it makes sense to autodereference in this case. It is seldom the right answer to have a pointer to a map, so there's no point in providing syntactic sugar for it. |
This is not a compiler issue. This is a language issue. The language does not permit the compiler to make this automatic dereference. Automatic dereference is only defined for selectors (https://golang.org/ref/spec#Selectors) and for array (not slice) indexes (https://golang.org/ref/spec#Index_expressions). Closing this issue. (You could file a language change proposal for this -- see https://golang.org/s/proposal -- but as pointer-to-map types are rarely used I don't think there would be much support for such a change.) |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I wrote code that looked like this:
What did you expect to see?
I expected it to work.
What did you see instead?
Additional info
Changing the code to:
Since the compiler automatically dereferences
*struct
s in other places, why doesn't it dereferenceb[key]
as well? Does it not have enough information?The source code was gotten from StackOverflow: Go: invalid operation - type *map[key]value does not support indexing. I have some similar though more complicated code.
The text was updated successfully, but these errors were encountered: