Skip to content

proposal: maps: add maps.KeysSlice and maps.ValuesSlice #61626

Closed
@cuiweixie

Description

@cuiweixie

Since we need to reserve Keys and Values for iterators.
I think we might need to change the old maps.Keys/Values to maps.KeysSlice/ValuesSlice as mention in 513715

Activity

added this to the Proposal milestone on Jul 28, 2023
moved this to Incoming in Proposalson Jul 28, 2023
AndrewHarrisSPU

AndrewHarrisSPU commented on Jul 30, 2023

@AndrewHarrisSPU

Does anyone like CloneKeys() []K, CloneValues() []V?

I'm not sure I'm not bike-shedding, but Clone might communicate the allocation of the returned slice, and the shallow copy semantics. slices uses Clone to mean shallow copy.

andig

andig commented on Aug 4, 2023

@andig
Contributor

Does anyone like CloneKeys() []K, CloneValues() []V?

It does not feel necessary. It's (almost) obvious that a map type object does not have its key and values in slices.

rsc

rsc commented on Aug 9, 2023

@rsc
Contributor

#61900 adds Keys and Values back as iterators.
#61899 adds slices.Collect and slices.Sorted.

With these, the old maps.Keys(m) would become slices.Collect(maps.Keys(m)) and similarly Values.
Better, this idiom:

keys := maps.Keys(m)
slices.Sort(keys)
use(keys)

becomes

use(slices.Sorted(maps.Keys(m))

Are there other pattern uses for maps.Keys (other than sorting them) that we should be sure to cover?

earthboundkid

earthboundkid commented on Aug 9, 2023

@earthboundkid
Contributor

Just my 2¢, but yeah, I feel like as long as slices.Sorted gets added, the uses of maps.KeySlice are infrequent enough that it could just be keys := slices.Append(make([]K,0, len(m)), maps.Keys(m)) those times when you need it.

rsc

rsc commented on Aug 9, 2023

@rsc
Contributor

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

moved this from Incoming to Active in Proposalson Aug 9, 2023
willfaught

willfaught commented on Aug 15, 2023

@willfaught
Contributor

Are there other pattern uses for maps.Keys (other than sorting them) that we should be sure to cover?

I suppose creating a set of them using a map[K]struct{}.

earthboundkid

earthboundkid commented on Aug 15, 2023

@earthboundkid
Contributor

You could do that with an iterator and InsertInto.

rsc

rsc commented on Aug 30, 2023

@rsc
Contributor

Finishing this proposal discussion is blocked on #61405.

14 remaining items

moved this from Likely Decline to Declined in Proposalson Feb 8, 2024
andig

andig commented on Jun 30, 2024

@andig
Contributor

Having to write

keys := slices.Append(make([]K,0, len(m)), maps.Keys(m))

to avoid excessive allocations still seems really cumbersome and little approachable to new devs. Given this would be best practice for performance (as @earthboundkid said "those times when you need it"), it couldn't hurt to make this easier to use other than resorting to x/exp/maps.

fzipp

fzipp commented on Jun 30, 2024

@fzipp
Contributor

Maybe, but why not wait a couple of years and then analyse the Go corpus to determine if it warrants an addition to the standard library.

it couldn't hurt to make this easier to use other than resorting to x/exp/maps.

Every addition to the standard library hurts. The question is whether the benefits outweigh this hurt.

andig

andig commented on Jun 30, 2024

@andig
Contributor

Imho the argument is above. Writing code that doesn't allocate more than necessary seems worth it imho and hasn't been specifically mentioned before.

fzipp

fzipp commented on Jun 30, 2024

@fzipp
Contributor

Imho the argument is above. Writing code that doesn't allocate more than necessary seems worth it imho and hasn't been specifically mentioned before.

It has been mentioned before:
#61900 (comment)
#61900 (comment)

andig

andig commented on Aug 3, 2024

@andig
Contributor

Decline is never permanent. Only accept is permanent.

Would this be a suitable proposal to be reopened? #68261 does not seem like an attractive alternative.

removed this from Proposalson Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @willfaught@rsc@andig@earthboundkid@fzipp

        Issue actions

          proposal: maps: add maps.KeysSlice and maps.ValuesSlice · Issue #61626 · golang/go