Misunderstaing the Language spec on Type parameter declarations

81 views
Skip to first unread message

Millicent Caputitl

unread,
Jun 22, 2022, 6:22:58 PM6/22/22
to golang-nuts
The ending statement on the Language spec on section Type parameter declarations, states  Type parameters may also be declared by the receiver specification of a method declaration associated with a generic type.
Does it mean something like this?
type I interface {
string | int
}

func Double[T I](value T) {
fmt.Println(value)
}

Ian Lance Taylor

unread,
Jun 22, 2022, 6:50:49 PM6/22/22
to Millicent Caputitl, golang-nuts
No, it means something like

type Slice[E any] []E
func (s Slice[E]) First() E { return s[0] }

The receiver specification of the method First is Slice[E], which
declares the type parameter E.

Ian
Reply all
Reply to author
Forward
0 new messages