Skip to content

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

Closed
gopherbot opened this issue May 13, 2010 · 11 comments
Closed

Comments

@gopherbot
Copy link
Contributor

by ehog.hedge:

What steps will reproduce the problem?
1. Attempt to compile

func main() {
    m := make(map[interface{}]interface{})
    m["key"] = 17
}

What is the expected output? 

Successful compilation.

What do you see instead?

invalid map index "key" - need type interface { }

What is your $GOOS?  $GOARCH?

amd64, linux

Which revision are you using?  (hg identify)

92e9a28d5886+ tip

Please provide any additional information below.

Introducing

  var s interface{} = "key"

and writing

  m[s] = 17

works. Defining

  type Any interface{}

and using

    m[Any("key")] = 17

produces

internal compiler error: typename ideal string

(Note: tried m[interface{}("key")] = 17; fails with a syntax
error, "unexpected interface", don't know whether this is a
bug or not, haven't looked yet.)
@griesemer
Copy link
Contributor

Comment 1:

Labels changed: added languagechange.

Status changed to Thinking.

@griesemer
Copy link
Contributor

Comment 2:

Owner changed to [email protected].

@rsc
Copy link
Contributor

rsc commented May 21, 2010

Comment 3:

I thought there was already an open issue for this but couldn't find it.
We've certainly discussed making the key expression only need to be assignment
compatible to the map key type.  It seems reasonable.

@griesemer
Copy link
Contributor

Comment 4:

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.

@griesemer
Copy link
Contributor

Comment 5:

While this is not accepted:
x := interface{}("key")
this one is:
x := interface{}(string("key"))

Owner changed to [email protected].

@rsc
Copy link
Contributor

rsc commented Jun 9, 2010

Comment 6:

This issue was closed by revision 565b5dc.

Status changed to Fixed.

@JeronimoColonSP
Copy link

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?

  1. Attempt to compile
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?

@randall77
Copy link
Contributor

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.

@griesemer
Copy link
Contributor

@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.)

@JeronimoColonSP
Copy link

@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.

@griesemer
Copy link
Contributor

@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.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants