Can I add type parameters to golang methods?

Viewed 424
type Vec[T any] []T

func NewVec[T any](cap int) Vec[T] {
    return make([]T, 0, cap)
}

func (v Vec[T]) Len() int {
    return len(v)
}


func (v Vec[T]) Map[U any](f func(T) U) Vec[U] {
    mapped := NewVec[U](v.Len())
    for _, elem := range v {
        mapped = append(mapped, f(elem))
    }
    return mapped
}

this is my code, compiler complain "method must have no type parameters" for the Map method, is there any way to bypass this error?

0 Answers
Related