by mseepgood · 6y ago · 0 comments
It does not require it, the author chose to do it.
t1 := hashtable.New(8, func(key string, m int) int {
is perfectly valid. type checking failed for main
prog.go2:17:3: cannot infer V (prog.go2:46:27)
prog.go2:22:3: cannot infer V (prog.go2:46:27)
But generally you are right, yes. It is able to infer K at least. func Filter(type T)(s []T, f func(T) bool) []T { ... }
s:= []int{1,2m3}
evens := slices.Filter(s, func(i int) bool { return i%2 == 0 })
The type of predicate is already in generic definition, we don’t need this verbosity. Other languages (I’d even say most of languages, used in dev work today) let us write simply something like slices.Filter(s, i->i%2).