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: maps: remove package for Go 1.21 #61613

Closed
zephyrtronium opened this issue Jul 27, 2023 · 8 comments
Closed

proposal: maps: remove package for Go 1.21 #61613

zephyrtronium opened this issue Jul 27, 2023 · 8 comments

Comments

@zephyrtronium
Copy link
Contributor

zephyrtronium commented Jul 27, 2023

#61538 removed maps.Keys and maps.Values from Go 1.21 pending #61405. However, a number of users suggested that Keys and Values are the most valuable pieces of package maps, and not having them available in the standard library can cause friction, especially with automatic import management in e.g. goimports and gopls.

This proposal is to hold back the entire package instead of only Keys and Values. Users who want its functionality can continue to import x/exp/maps.

@earthboundkid
Copy link
Contributor

Just repeating myself from the other issue: We might want maps.Copy to take an iterator as its second argument, in which case, it should also be held back from 1.21 just in case.

@heschi heschi modified the milestones: Proposal, Go1.21 Jul 27, 2023
@willfaught
Copy link
Contributor

willfaught commented Jul 28, 2023

Could this be resolved by having a special naming convention for iterator variants of allocating functions? E.g. we could ship maps.Keys now, and add maps.KeysIter later?

Edit: The reason for pulling maps.Keys was that it forces allocation, but returning an iterator imposes the opposite problem: what if I want a slice of keys? I'd have to allocate the slice, iterate through and build up the slice, etc. I'm not sure there's a right "default" approach here; both variants are useful.

@DeedleFake
Copy link

DeedleFake commented Jul 28, 2023

Building a slice from an iterator doesn't necessarily require any extra allocation:

package iters

func Append[T any](s []T, iter Iter[T]) []T {
  for v := range iter {
    s = append(s, v)
  }
  return s
}

Then you can just append the maps.Keys() iterator directly into a preallocated slice. It'll essentially do what maps.KeysSlice() would do internally.

That being said, I do think that maps.KeysSlice() should exist, as otherwise the one-line way of getting a slice of the keys would be

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

which is really kind of awkward.

@AndrewHarrisSPU

This comment was marked as off-topic.

@fzipp
Copy link
Contributor

fzipp commented Jul 30, 2023

@AndrewHarrisSPU
The issue for discussion of functions to return map keys and values as slices is #61626.

@nemith
Copy link
Contributor

nemith commented Aug 2, 2023

My initial reaction to this was this seemed a bit heavy handed, but the more I thought about it in relation to the range over function proposal and it's impact over existing data containers got me thinking.

One of the the things that I think, @ianlancetaylor say at one point is, it would be nice to define additional map types using generic type parameters instead of special builtin types defined only in the runtime. For example there could be a OrderedMap[K, V] or a equivelent of rusts BtreeMap. These are do-able today with type parameters, but become much more powerful when we can have them be natively iterated over by #61405.

Would a the existing maps be able to handle custom types like these in the future or should that even be a consideration at this time? My guess is custom types and the builtin type are going to be too different to have generic functions that work on both, but I haven't put much thought into it.

@rsc
Copy link
Contributor

rsc commented Aug 2, 2023

Pulling all of maps seems strictly more disruptive than pulling just Keys and Values.
There are no concerns about the remaining API.
Copy is a fine name for what it does.
People who need both Equal and Keys can continue to import one package, namely golang.org/x/exp/maps.

@rsc
Copy link
Contributor

rsc commented Aug 2, 2023

No change in consensus, so declined.
— rsc for the proposal review group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Declined
Development

No branches or pull requests

10 participants