On Mon, Dec 28, 2020 at 4:30 AM redsto...@gmail.com <redstorm....@gmail.com> wrote: > > 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.
There have been similar proposals before. Interface-only contracts do not work for primitive types, as you realized, so there were suggestions to work around that limitation, as you already did. I believe the question you should ask is that in a language with no operator overloading, what is the purpose of defining a constraint that allows operator >? That simply means numeric types and string. I think it is much more concise and explicit to say "the function expects a numeric or string" than to say "the function expects a type on which > can be used". > > 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 golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/debe7749-fe96-4587-814b-a76ee7f48528n%40googlegroups.com. -- 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 golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2Rqpa9vnPcLuxyHUdNME0Cw9%2BK1wSDhYp4Qqp_S6bZ4_Ong%40mail.gmail.com.