Thanks! This seems obvious now just looking at it, but it was stumping me. I might add for someone that may see this later, in the future it *might* not be needed to do it this way, if all you really just want {Int | Float | Rat} as future versions are supposed to let us use methods in such constrained types if a method is present in all the types, like Cmp in this case, but I'm not sure if it clear that >1.18 will handle this particular case where it is only the same method signature if you note that the param type is the same as the method receiver type.
On Sun, Apr 10, 2022 at 11:23 AM Axel Wagner <axel.wagner...@googlemail.com> wrote: > You can use > > type Comparable[T any] interface { > Cmp(other T) int > } > > then *big.Int implements `Comparable[*big.Int]` and you can write > > func cmpSort[T Comparable[T]](x []C) { … } > > On Sun, Apr 10, 2022 at 7:48 PM Shawn Smith <sycomon...@gmail.com> wrote: > >> // ... imagine some comparison sort algorithm >> >> func cmpSort[C Cmpable](x []C) { >> for i, val := range x { >> if i == 0 { >> continue >> } >> _ = val.Cmp(x[i-1]) >> } >> } >> >> // Cmpable should include big.Int, big.Float, big.Rat, by virtue of their >> Cmp methods. So how do i define this better? >> >> type Cmpable interface { >> Cmp(other any) int >> } >> // TODO: how do i replace "any" with "this type". "Cmpable" doesn't work >> as that would be any Cmpable, not THIS Cmpable. >> >> -- >> 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/4f167deb-bf63-43db-9ec9-c4c451c14d09n%40googlegroups.com >> <https://groups.google.com/d/msgid/golang-nuts/4f167deb-bf63-43db-9ec9-c4c451c14d09n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- 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/CAMC8FUo%2BCA9ODLz%3D6crU8Xn-gww%3Dadcc0EmZ8LPRsocb4mJ8YQ%40mail.gmail.com.