proposal: maps: new package to provide generic map functions (discussion) #47330
Replies: 22 comments 100 replies
-
[ Resolved -- changed to
I could see a |
Beta Was this translation helpful? Give feedback.
-
[ Resolved -- these are just maps, we don't need to define performance beyond that ] Same performance note as in sets: #47331 (comment) |
Beta Was this translation helpful? Give feedback.
-
[ Resolved -- changed to I have a mild preference for renaming |
Beta Was this translation helpful? Give feedback.
-
[ Resolved - Other language have I have a preference to choose |
Beta Was this translation helpful? Give feedback.
-
I can see the benefit to Thinking ahead to that, should we name these functions |
Beta Was this translation helpful? Give feedback.
-
Would you mind adding an example of what it would look like to use it? |
Beta Was this translation helpful? Give feedback.
-
[ Resolved -- leave for later ] Along with the |
Beta Was this translation helpful? Give feedback.
-
[ Resolved -- leave for later ] How about an If that gets added, it might also make sense to add |
Beta Was this translation helpful? Give feedback.
-
[ Resolved -- the existence of modules doesn't mean that the standard library is not needed ] Now as Go supports modules I wonder why do you want to put these generic slices and maps packages into standard library. Why not putting it to |
Beta Was this translation helpful? Give feedback.
-
[ Resolved -- no ] Would this also work with |
Beta Was this translation helpful? Give feedback.
-
Retracted I would like to propose adding
On its own, this isn't very useful - we can just use It would also lay the groundwork for an iteration API - for example, a package could define type Iter[V any] interface {
Next() bool
Value(p *V)
} to be able to iterate over either a map or a Lastly, this is something that can't really be implemented outside of the stdlib. It requires interaction with the runtime and its implementation details. |
Beta Was this translation helpful? Give feedback.
-
[ Resolved -- leave for later ] If there should be an func CloneFunc[K comparable, V1, V2 any](m map[K]V1, transform func(V1) V2) map[K]V2 |
Beta Was this translation helpful? Give feedback.
-
[ Resolved -- out of scope for this proposal ] I think it would be extremely valuable if the standard library contained a standardized iterator interface, something like: type Iterator[T any] interface {
// Next returns true until there are no more elements in this iterator.
func Next() bool
// Value returns the current value of this iterator.
func Value() T
} It would allow to get the keys or values of a map without allocating a slice. keys := maps.Keys(myMap);
for keys.Next() {
fmt.Println(keys.Value())
} If putting the values in a slice is required, a function to do this can be created easily in the func Collect[T any](it Iterator[T]) []T {
var result []T
for it.Next() {
result = append(result, it.Value())
}
return result
} I don't think language support for Iterators is necessary, but I think that defining a standard interface for them is a necessity. If no official interface for iterators is provided, it's almost certain that multiple implementations will pop up in 3rd party libraries and lead to fragmentation of the ecosystem. Plus it allows us to create potentially less memory intensive programs by avoiding slice allocations. |
Beta Was this translation helpful? Give feedback.
-
DeleteIf still doesn't feel exactly right to me. It occurs to me that slices, bytes, and strings all have a precedent for this kind of thing already: IndexFunc, ContainsFunc, and so on. It sure seems like slices.Index is to slices.IndexFunc as maps.Delete is to __this function__, which suggests it should be called DeleteFunc. Here are all the functions taking functions as arguments in the standard library:
Obviously many of these aren't analogous, but the ones are analogous overwhelmingly use FooFunc. I suggest we use DeleteFunc. |
Beta Was this translation helpful? Give feedback.
-
I believe it will be very valuable to add a function like |
Beta Was this translation helpful? Give feedback.
-
Adding a method to create immutable readonly |
Beta Was this translation helpful? Give feedback.
-
Just realized: The top-comment doesn't use |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
I'm concerned about the complexity of these function signatures. It's definitely better now that we can use For reference, here's the current signature:
The simpler signature:
Will be simpler to read in the docs, and actually uses The only downside it has is what @Merovius pointed out above -- if you need to instantiate or pass around a function of the concrete type. (An example @rogpeppe showed me is here). As Axel acknowledged above, "it's a bit of a stretch". I think this will be very rare, and I believe doesn't warrant complicating the signatures -- normal use will just be a type-inferred call like The only function that really needs it is There was discussion about avoiding |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
Having used the It's not unusual to have an interface as key types in a map, which however immediately means not being able to use these functions. This requirement of having I understand that this might boil down to the argument of |
Beta Was this translation helpful? Give feedback.
-
One classic example is |
Beta Was this translation helpful? Give feedback.
-
This is a discussion that led to the proposal #47649.
This proposal is for use with #43651. We propose defining a new package, maps, that will provide functions that may be used with maps of any type. If this proposal is accepted, the new package will be included with the first release of Go that implements #43651 (we currently expect that that will be Go 1.18).
This description below is focused on the API, not the implementation. In general the implementation will be straightforward.
See also the slices proposal at #45955 (discussion at #47203).
Beta Was this translation helpful? Give feedback.
All reactions