Hello Go users!
I am playing with the generics that were added in go 1.18 and noticed an
odd discrepancy that I have not been able to find an authoritative answer
to. Consider the following data type, as well as the function defined for
it:
type Node[T any] struct {
Inner T
Next *Node[any]
}
func (n *Node[any]) ListPreTraverse(f func(*Node[any])) {
tmpN := interface{}(n)
if tmpN, ok := tmpN.(*Node[*Node[any]]); ok {
tmpN.Inner.ListPreTraverse(f)
} else {
f(n)
}
n.Next.ListPreTraverse(f)
}
This function is a simple attempt to iterate across a generic linked list
and call a function on each node in the list (although in this case it may
behave more like a tree if inner stores another node). When I went to
compile this code I encountered the following error:
test.go:64:25: cannot use f (variable of type func(*Node[any])) as type
func(*Node[any]) in argument to n.Next.ListPreTraverse
To me at least it seems that all the information to validate that the
argument fits the type specified in the function call is there, defined in
the function header. What am I missing?
Thanks,
Ava
--
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/951bc5d6-6194-4cfa-905d-66f7f0059a65n%40googlegroups.com.