I came across this stack overflow question:
https://stackoverflow.com/questions/71131665/generics-pass-map-with-derived-types

The OP attempted to pass different maps into this code:

func equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool { if 
len(m1) != len(m2) { return false } for k, v1 := range m1 { if v2, ok := 
m2[k]; !ok || v1 != v2 { return false } } return true }

It is clear to me why this doesn't work. However, I was studying possible 
fixes for this function. Here's a playground: 
https://gotipplay.golang.org/p/NPpau1G0Hqp

My first attempt was based on differentiating the K and V type params and 
came up with this:

func equal[M1 ~map[K1]V1, M2 ~map[K2]V2, K1, K2 ~uint32, V1, V2 ~string](m1 
M1, m2 M2) bool {
        if len(m1) != len(m2) {
                return false
        }
        for k, v1 := range m1 {
                if v2, ok := m2[K2(k)]; !ok || V2(v1) != v2 {
                        return false
                }
        }
        return true
}

I thought this *should work*, because now it's possible to infer K1 and K2 
from M1 and M2 respectively, and the approximate constraints allow 
conversion in the function body. But it still doesn't compile with error 
"K2 does not match uint32". 

Instead, this works, even though K1 and K2 are defined the same way:

func equalFixed[K1, K2 ~uint32, V1, V2 ~string](m1 map[K1]V1, m2 map[K2]V2) 
bool {
}

By reading the Go 1.18 language specs, my intuition is that the first 
attempt fails due to the so-called "adjusted core type" of the constraint 
~uint32, though I'm not 100% sure of what's going on. 
Can you folks confirm my intuition and/or provide some pointers?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/d579177e-592c-40bf-88ef-90d10ee5bac1n%40googlegroups.com.

Reply via email to