Maps and Slices don't have a lot of operations in common. In general, the only ones I can think of are - `make(T, someInt)`, though it means *very* different things for both, so a generic function doing this is likely not super sensible - `for _, v := range x`, though even this differs, as the order is not guaranteed to be deterministic for maps, but it is for slices - `x == nil`, `len(x) == 0`
If you'd restrict the key-type of the map to be of integer type, there's one (?) more - `x[i]` for the key-type. But only on the RHS, as it's not addressable for maps. I might overlook something, but in any case, the set of operations a generic function could do with an argument constrained like this is extremely limited. I guess the most useful way to express the constraint would be type SliceOrMap(type K constraints.Integer, V interface{}) interface { type map[K]V, []V } But really, given that this is such a useless constraint, I feel it's fine that it's hard to express nicely. On Sat, Jun 20, 2020 at 9:08 PM David Finkel <david.fin...@gmail.com> wrote: > > > On Sat, Jun 20, 2020 at 11:03 AM T L <tapir....@gmail.com> wrote: > >> >> >> On Saturday, June 20, 2020 at 10:21:56 AM UTC-4, Axel Wagner wrote: >>> >>> I would assume it's >>> >>> type MapConstraint(type K comparable, V interface{}) interface { >>> type map[K]V >>> } >>> >>> func F(type M MapConstraint(K, V), K comparable, V interface{}) (m M) { >>> } >>> >>> Note that you are under no obligation to make use of a type-parameter if >>> you don't need it. >>> >> >> I don't very understand this. Can a slice value be used as the argument >> of the F function? >> > For that, I think you'd need the interface to be: > type MapSliceConstraint(type K comparable, V interface{}) interface { > type map[K]V, []V > } > > I'm not quite sure how to eliminate the useless K type-param for slices, > though. > > Note: the relevant draft design section is: > https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#type-parameters-in-type-lists > > Here's an almost working example: > https://go2goplay.golang.org/p/qcdfl0tuHlb > > It looks like there's a bug in the type-parameter constraint checking > because in the above example code, I get: > > type checking failed for main > > prog.go2:11:39: K does not satisfy comparable > > This, despite the type parameter definition literally requiring that K be > comparable: > func genLen(type T MapSliceConstraint(K, V), K *comparable*, V > interface{})(collection T) int { > return len(collection) > } > (example without a main() to reduce clutter: > https://go2goplay.golang.org/p/1gqiYuDELuI) > >> >> >>> >>> On Sat, Jun 20, 2020 at 4:14 PM T L <tapi...@gmail.com> wrote: >>> >>>> I mean I don't care about the element and key types of the parameter >>>> type. >>>> >>>> For a simple example, I want to define a generic function which prints >>>> the length of a container (do do some other things): >>>> >>>> func Print(type T Container) (v T) { >>>> // ... do some things >>>> >>>> fmt.Println(len(v)) >>>> >>>> // ... do some things >>>> } >>>> >>>> On Saturday, June 20, 2020 at 9:16:39 AM UTC-4, T L wrote: >>>>> >>>>> . >>>>> >>>> -- >>>> 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 golan...@googlegroups.com. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/golang-nuts/4f42e5e9-f991-4ee7-a043-5350a62f787fo%40googlegroups.com >>>> <https://groups.google.com/d/msgid/golang-nuts/4f42e5e9-f991-4ee7-a043-5350a62f787fo%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/f841c497-8c29-4f16-a027-2c446f1a94b6o%40googlegroups.com >> <https://groups.google.com/d/msgid/golang-nuts/f841c497-8c29-4f16-a027-2c446f1a94b6o%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/CANrC0BjPALDX6%2BT-tCpH3aaLSLQqQb7LExCzCuzL5uPGFxgxJw%40mail.gmail.com > <https://groups.google.com/d/msgid/golang-nuts/CANrC0BjPALDX6%2BT-tCpH3aaLSLQqQb7LExCzCuzL5uPGFxgxJw%40mail.gmail.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/CAEkBMfGcoxSycZhCAfZjtwv_iBJx%3DQ0KZWxM-UwjOs8bLTDi1Q%40mail.gmail.com.