-
Notifications
You must be signed in to change notification settings - Fork 18k
map with interface{} key does not accept concretely-typed key expressions #778
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
Comments
Owner changed to [email protected]. |
This should be legal according to the spec ( http://golang.org/doc/go_spec.html#Indexes ). There are 2 compiler errors: 1) it should accept: m["key"] = 17 // according to spec on indexing of maps 2) it should accept: x := interface{}(17) // according to new spec on conversions (17 is assignment compatible) This code runs as expected: func main() { m := make(map[interface{}]interface{}) var x interface{} = "key" m[x] = 17 } Labels changed: added compilerbug, removed languagechange. Status changed to Accepted. |
While this is not accepted: x := interface{}("key") this one is: x := interface{}(string("key")) Owner changed to [email protected]. |
This issue was closed by revision 565b5dc. Status changed to Fixed. |
I _think_ I have an example that meets the description of the issue but difference reproduction steps. I'll follow the original posters format: What steps will reproduce the problem?
package main
func main() {
m := map[string]int{"me": 1}
TestFunc(m)
}
func TestFunc(m map[interface{}]interface{}) {
println(m)
} This seems similar to the original issue but I could be way off (I'm new to Go). Thoughts? |
Your example is not a legal program. TestFunc takes a concrete map type, to be assignment compatible to that type it must be the exact type map[interface{}]interface{}. This bug is about assignment compatibility of keys of maps with interface{} as the key type, not about assignment compatibility of maps. |
@JeronimoColonCTCT Your example is different. You are passing a map of a different type (map[string]int) as as argument to a parameter of type map[interface{}]interface{}. In contrast to other languages, in Go types have to match much more closely, there's no contravariance in play when passing arguments, and there's no implicit dynamic checking either. This doesn't and shouldn't compile and is unrelated to the issue at hand that was fixed a while back. (The issue is about concrete key values used in a map with interface{} key type. Your example is about assigning a map of one type to a map of another.) |
@griesemer, I see. I grossly misunderstood the use of interface {} in the map example and type passing in this context. After reading your response I clearly see why this would not work. Not sure why I even thought it would in the first place (how embarrassing). What I conflated, in my head, was my example and: func TestFunc(m interface{}) {
...
} Like I said new to Go (and Github Issues format)... I can delete my post if that's acceptable. Thanks. |
@JeronimoColonCTCT We usually keep the posts - of course if you rather not have it, it's up to you. Keep in mind that everybody was new to Go at some point and made mistakes. |
by ehog.hedge:
The text was updated successfully, but these errors were encountered: