On Tue, Aug 4, 2020 at 9:19 PM Robert Engels <reng...@ix.netcom.com> wrote: > > The operator support is what makes things so complicated. Why not define a > “numeric” type that has methods for all the operators that is used with > generics, similar to Number in Java. Then it is unified. If you are writing > specialized math performant code you would probably use concrete types anyway. > > People complained about the boxing of primitives in Java but it was rarely a > problem in actual use - and then people fell back to specialized structures > anyway - which is what would happen here.
Numbers are not the only primitive types in Go. Ian > > On Aug 4, 2020, at 11:06 PM, Ian Lance Taylor <i...@golang.org> wrote: > > > > On Tue, Aug 4, 2020 at 7:31 PM Ben Hoyt <benh...@gmail.com> wrote: > >> > >>> Yes, I think that the ability to write generics using operators is not > >>> optional. As I've said before, there are some functions that must be > >>> possible to write using any plausible generics mechanism. One of them > >>> is the Min function that returns the smaller of two values of some > >>> ordered type. If we can't write that trivial function, our generics > >>> mechanism is not usable for practical code. > >> > >> I don't think that's quite true. You can write it, it's just not as > >> nice to use with builtin types: > >> > >> func Min[type T Lesser](a, b T) T { > >> if a.Less(b) { > >> return a > >> } > >> return b > >> } > >> > >> To use it with (for example) int, you need to write a wrapper type, for > >> example: > >> > >> type Int int > >> func (i Int) Less(j Int) bool { return i < j } > >> > >> And then call it like so: > >> > >> Min(Int(2), Int(3)) > >> > >> It's not pretty, but it definitely works > >> (https://go2goplay.golang.org/p/i6Q3tF-QwgH). This was kind of the > >> situation with sort.Sort (until sort.Slice came along), so it doesn't > >> seem any worse than that. Or you could instead define a generic type > >> "Ord" instead of Int: > >> > >> type Ord[type T Ordered] T > >> func (i Ord[T]) Less(j Ord[T]) bool { return i < j } > >> > >> And use it like so: > >> > >> Min(Ord[int](2), Ord[int](3)) > >> > >> Or if type inference were improved to handle that case: > >> > >> Min(Ord(2), Ord(3)) > >> > >> It's not pretty, partly because you get an Int/Ord result instead of a > >> plain int, but not terrible either. > >> > >> So it seems to me it's not a case of "we can't write that trivial > >> function" -- it just requires a bit of type conversion. > > > > OK, fair enough. > > > > I'll be more specific: I think it's necessary that it be possible to > > write a Min function whose body is something similar to > > > > if a < b { > > return a > > } > > return b > > > > I think that any approach to generics that requires more complex code > > would simply be rejected by the community of Go programmers. > > > > > >> So either way > >> -- with your approach or this approach -- it's klunky for one of the > >> cases: > >> > >> 1) Assuming type lists and a function which takes a comparator func > >> like "func Min[type T](a, b T, less func(x, y T) bool) T" -- in this > >> case you have the klunkiness of having to import (or define) > >> "primitives" and specify the comparison func even when it's obvious: > >> Min(2, 4, primitives.Less) > >> 2) With no type lists and a function which takes a "lesser" like "func > >> Min[type T Lesser](a, b T) T" -- in this case you have the klunkiness > >> of having to use wrapper types, like we did with sort.Sort back in the > >> day. > > > > To be clear, when I discussed a function, I was only discussing that > > in contrast to a type with a required method. I did not mean to > > suggest that a function would be suitable for use with a primitive > > type that supports operators. > > > > Ian > > > > -- > > 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/CAOyqgcWqoACJp97ZZhKecNVg-OdxVoUxNX_kZusp2-xmZRwnmw%40mail.gmail.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/CAOyqgcV-96Ed3GQjxbsWX_ksJiKFu6JDQ0F6COYMLAp6G-Txtw%40mail.gmail.com.