The error message is confusing because the "any" used in
ListPreTraverse is shadowing the "any" used in Node. See
https://go.dev/doc/faq#types_in_method_declaration. You want to write
func (n *Node[T]) ListPreTraverse(f func(*Node[T])) {
But then you will see that your code has an instantiation cycle,
violating the restriction described at
https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#generic-types:
"A generic type can refer to itself in cases where a type can
ordinarily refer to itself, but when it does so the type arguments
must be the type parameters, listed in the same order. This
restriction prevents infinite recursion of type instantiation."
I don't really understand what you are trying to do here. The type
assertion doesn't make sense to me. Perhaps this is the code you
want:
type Node[T any] struct {
Inner T
Next *Node[T]
}
func (n *Node[T]) ListPreTraverse(f func(*Node[T])) {
f(n)
if n.Next != nil {
n.Next.ListPreTraverse(f)
}
}
Ian