golang std lib deficiency
Sep. 1st, 2022 01:37 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Suppose one is writing Go - not the worst fate, but not exactly the best fate. One problem -
https://go.dev/play/p/po5A8KG6Zpv?v=goprev
there's no reason why a generic Merge1 and MergeRecursive function shouldn't be implemented.
Note that this essentially requires `interface{}` as the value type.
The actual V type should be the unification of both input types: if that can't be resolved intelligibly, then barf.
Part of the issue here is that the type system _should_ (but doesn't) have tagged union types. That would be a reasonably elegant solution to this.
https://go.dev/play/p/po5A8KG6Zpv?v=goprev
package main import "fmt" func MapMerge1[K comparable, V any](a map[K]V, b map[K]V) map[K]V { m := map[K]V{} for k, v := range a { m[k] = v } for k, v := range b { m[k] = v } return m } func main() { disabledThing := map[string]interface{}{ "enabled": false, } other := map[string]interface{}{ "options": map[string]interface{}{ "thungus": false, }, } merged := MapMerge1(disabledThing, other) fmt.Println(merged) }
there's no reason why a generic Merge1 and MergeRecursive function shouldn't be implemented.
Note that this essentially requires `interface{}` as the value type.
The actual V type should be the unification of both input types: if that can't be resolved intelligibly, then barf.
Part of the issue here is that the type system _should_ (but doesn't) have tagged union types. That would be a reasonably elegant solution to this.