pnathan: elephant bypasses fence to drink from pool (Default)
[personal profile] pnathan
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


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.

Most Popular Tags

Expand Cut Tags

No cut tags
Page generated Jun. 12th, 2025 12:18 pm
Powered by Dreamwidth Studios