On Jun 16, 2020, at 7:06 PM, Brandon Dyck <bran...@dyck.us> wrote: > > I find it a little strange that an interface with a type list can only be > used as a constraint, and not as the type of a variable or parameter, despite > it using basically the same syntax as a normal interface. Is this difference > between constraints and other interfaces a property of the type system, or > just a limitation of the translation? I don't think it was explicit in the > design document. It would certainly be useful to declare variables with > type-list interfaces, as it would provide a much less hacky way to define sum > types than using an unexported interface method as in > https://medium.com/@haya14busa/sum-union-variant-type-in-go-and-static-check-tool-of-switch-case-handling-3bfc61618b1e.
This use is not quite the same as sum types. Basically the use of a type list in 'ordered' *stands in* for types that have ordered comparison operators but Go doesn't have a way to specify this. And even this did not work for the 'comparable' as the list is open ended. Both seem to have resulted from Go's peculiarity. In comparison a sum type can contain any types. These types need not have anything in common. As long as an operation is qualified with a type assertion this works. Seems to me that perhaps 'ordered' should also be *predeclared*. What happens if/when a big int type is added in future? Or decimal or big floats. "The Expression Problem" comes to mind :-) It would be nice if it was possible to extend operators for user defined types (but I suspect people will object!). If syntax for such existed, both 'ordered' and 'comparable' could be defined cleanly. I don't really see very many uses of an interface containing a list of types. Finally how would generics defined using 'ordered' deal with NaNs? NaNs are unordered! -- 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/C4CCD5F9-AFD3-462B-B1BC-C127F9EB1884%40iitbombay.org.