-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Closed
Labels
Description
What version of Go are you using (go version
)?
$ go version go version devel go1.18-9d0ca262bb Wed Dec 15 00:33:55 2021 +0000 darwin/amd6
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env
What did you do?
https://go.dev/play/p/l9gzFUOBT5W?v=gotip
package main
import (
"fmt"
"strconv"
"strings"
)
type StringParser[T any] func(s string) (T, error)
func ParseList[T any](p StringParser[T], s string) ([]T, error) {
if s == "" {
return nil, nil
}
parts := strings.Split(s, ",")
vals := make([]T, len(parts))
for i, part := range parts {
v, err := p(part)
if err != nil {
return nil, err
}
vals[i] = v
}
return vals, nil
}
func (p StringParser[T]) ToListParser() StringParser[[]T] {
return func(s string) ([]T, error) {
return ParseList(p, s)
}
}
func ToListParser[T any](p StringParser[T]) StringParser[[]T] {
return func(s string) ([]T, error) {
return ParseList(p, s)
}
}
func main() {
p := StringParser[int](strconv.Atoi).ToListParser()
fmt.Println(p("1,2,3"))
}
ToListParser
is fine to compile,, but the compiler reports errors on StringParser.ToListParser
, :
./prog.go:9:19: instantiation cycle:
prog.go:27:54: T instantiated as []T
as i can see, []T
is depends on T
, rather than StringParser[T]
, there shouldn't be a cycle.
I don't know whether this is a bug, or go doesn't allow this usage ?