If generic is inevitable, make it better. The type list in the current
draft is so special that it is added only for operators. I think it will
bring complexity and inconsistens in go. So I get an idea to replace it.
type bigger[T bigger] interface{
BiggerThan(T) bool
}
func max[T bigger[T]](a, b T) T{
if a.BiggerThan(b) {
return a
} else {
return b
}
}
type BiggerInt int
func (a BiggerInt) BiggerThan(b BiggerInt)bool{
return a > b
}
type BiggerFloat float32
func (a BiggerFloat) BiggerThan(b BiggerFloat)bool{
return a > b
}
max(1,2)
instead of operators we use method. the generic call site remain the same.
If we allow the implicity type conversion between BiggerInt and int, this
will work.
This solution will write more code when we define BiggerThan in each type.
But we remove type list in interface. How do you think it?
--
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/debe7749-fe96-4587-814b-a76ee7f48528n%40googlegroups.com.