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

proposal: reflect: rename MapIter.SetKey and MapIter.SetValue #48294

Open
dsnet opened this issue Sep 9, 2021 · 6 comments
Open

proposal: reflect: rename MapIter.SetKey and MapIter.SetValue #48294

dsnet opened this issue Sep 9, 2021 · 6 comments
Labels
Projects
Milestone

Comments

@dsnet
Copy link
Member

@dsnet dsnet commented Sep 9, 2021

(copying my comment from #46131 (comment) to avoid it being forgotten for the 1.18 release)

Bikeshed: I was a little confused by the direction of setting. In reflect, we have:

func (v Value) Set(x Value)
func (v Value) SetBool(x bool)
func (v Value) SetBytes(x []byte)
func (v Value) SetCap(n int)
func (v Value) SetComplex(x complex128)
func (v Value) SetFloat(x float64)
func (v Value) SetInt(x int64)
func (v Value) SetLen(n int)
func (v Value) SetMapIndex(key, elem Value)
func (v Value) SetPointer(x unsafe.Pointer)
func (v Value) SetString(x string)
func (v Value) SetUint(x uint64)

All of these store a value into the receiver v from the input argument.

Now we added:

func (it *MapIter) SetKey(dst Value)
func (it *MapIter) SetValue(dst Value)

However, contrary to reflect.Value.SetXXX, a value is being stored into the input argument from state in the receiver it. It is the opposite direction.

Perhaps we should rename it as SetKeyInto and SetValueInto? Or StoreKey and StoreValue (per @josharian's suggestion)?

\cc @rsc @randall77

@dsnet dsnet added this to the Go1.18 milestone Sep 9, 2021
@randall77
Copy link
Contributor

@randall77 randall77 commented Sep 9, 2021

Or even,

func (v Value) SetKey(it *MapIter)
func (v Value) SetValue(it *MapIter)

?

@dsnet
Copy link
Member Author

@dsnet dsnet commented Sep 9, 2021

I like that even better :)

@dsnet
Copy link
Member Author

@dsnet dsnet commented Sep 9, 2021

Although, you probably want to add Map in the name:

func (v Value) SetMapKey(it *MapIter)
func (v Value) SetMapValue(it *MapIter)

since the other map functionality have Map in the name: Value.MapIndex, Value.MapKeys, Value.MapRange, Value.SetMapIndex.

@ianlancetaylor ianlancetaylor changed the title reflect: rename MapIter.SetKey and MapIter.SetValue proposal: reflect: rename MapIter.SetKey and MapIter.SetValue Sep 9, 2021
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals Sep 9, 2021
@randall77
Copy link
Contributor

@randall77 randall77 commented Sep 9, 2021

Or SetIterKey/SetIterValue.

@dsnet
Copy link
Member Author

@dsnet dsnet commented Sep 11, 2021

I was looking at sources of allocations using reflect. Another that I found was something of the following pattern:

v.Set(m.MapIndex(k))

Ideally, we would be able to do this with an allocation-free API. Perhaps:

v.SetMapIndex(m, k)

Whatever names we chose here, I would hope that it doesn't steal a good name from this possible API.

@rsc rsc moved this from Incoming to Active in Proposals Sep 15, 2021
@rsc
Copy link
Contributor

@rsc rsc commented Sep 15, 2021

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

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

Successfully merging a pull request may close this issue.

None yet
4 participants