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: confusing example in map index expressions section #23895

Closed
quasilyte opened this issue Feb 17, 2018 · 4 comments
Closed

spec: confusing example in map index expressions section #23895

quasilyte opened this issue Feb 17, 2018 · 4 comments

Comments

@quasilyte
Copy link
Contributor

quasilyte commented Feb 17, 2018

From index expressions section:

An index expression on a map a of type map[K]V used in an assignment or initialization of the special form yields an additional untyped boolean value. The value of ok is true if the key x is present in the map, and false otherwise.

v, ok = a[x]
v, ok := a[x]
var v, ok = a[x]
var v, ok T = a[x] // <- This one. 4th example

It seems like 4th example is only valid when T is bool, which assumes that map have bool as element value type.

If this is correct, then var v, ok T = a[x] should be either var v, ok bool = a[x] or re-phrased in other way (perhaps even removed).

@yazsh
Copy link
Contributor

yazsh commented Feb 17, 2018

Considering the map is declared as value type of V using T below is confusing. I would either remove the example entirely or change it to V.

I don't see the added value in it's inclusion with respect to map indexing so I am suggesting it be removed.

The both code snippets below will not compile. The top snippet because you can't store an int in a bool, the bottom because it considers ok an int which cannot be used in an if statement.

k := make(map[string]int)

var v, ok  bool = k["asdf"]
	
k := make(map[string]int)

var v, ok  int = k["asdf"]

if(ok){
	fmt.Println(ok)
	fmt.Println(v)
	}

@gopherbot
Copy link

Change https://golang.org/cl/94906 mentions this issue: doc: improve clarity of map index examples

@dotaheor
Copy link

dotaheor commented Feb 18, 2018

I don't think it is much confusing. The type T is not essential to be a bool, it can be any boolean type (and which is also the map element type).
The 4th example make the cases completed. So keeping it is better than removing it.

@quasilyte
Copy link
Contributor Author

quasilyte commented Feb 18, 2018

@dotaheor, I see now. You are right, thank you.

The ok type is not bool, but untyped bool. Makes sense then.

This, for example, compiles and executes as expected:

	type mybool bool
	m := make(map[string]mybool)
	var v, ok mybool = m[""]
	fmt.Println(v, ok)

I believe issue can be closed then.

gopherbot pushed a commit that referenced this issue Feb 19, 2018
The fourth example for map indexing states you have a map of type [K]V
and attempts to read in a variable of type T.  Further, the example
is meant to showcase the boolean return variable saying whether the
map contained a key, but overrides to type T.  This will not compile.

Changed last updated date to February 18

Fixes: #23895

Change-Id: I63c52adbcd989afd4855e329e6c727f4c01f7881
Reviewed-on: https://go-review.googlesource.com/94906
Reviewed-by: Robert Griesemer <gri@golang.org>
@golang golang locked and limited conversation to collaborators Feb 18, 2019
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

4 participants