Closed
Description
What version of Go are you using (go version
)?
devel go1.18-d15481b8c7 Fri Jan 21 01:14:28 2022 +0000
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env
)?
Go playground (gotip mode)
What did you do?
func Merge[M ~map[K]V, K comparable, V any](ms ...M) M {
result := M{}
for _, m := range ms {
maps.Copy(result, m)
}
return result
}
https://go.dev/play/p/q50y-S0xAVm?v=gotip
What did you expect to see?
map[1:true]
What did you see instead?
./prog.go:14:12: M does not match map[K]V
Since M is ~map[K]V
by definition, this is confusing me. If I explicitly instantiate maps.Copy
to map[K]V
, it works:
https://go.dev/play/p/SL4LNhpR3jB?v=gotip
It also works as expected if I define Merge
this way:
func Merge[K comparable, V any](ms ...map[K]V) map[K]V {
But I noticed that the functions in maps
have a more elegant signature:
func Copy[M ~map[K]V, K comparable, V any](dst, src M) {
And indeed I can use this, until I try to call a generic function with some M.
Shouldn't the compiler be able to infer the instantiating type here? I'm not sure if this is related to #50484 or #50319, but this is neither an interface nor a function type.