[go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread T L
Or any map type with a specified key type but ignore the element type. 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 fr

[go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread T L
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)) // ...

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread 'Axel Wagner' via golang-nuts
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. On Sat, Jun 20, 2020 at 4

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread T L
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 ob

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread David Finkel
On Sat, Jun 20, 2020 at 11:03 AM T L 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

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread 'Axel Wagner' via golang-nuts
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 orde

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread Ian Lance Taylor
On Sat, Jun 20, 2020 at 12:08 PM David Finkel wrote: > > 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.

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread T L
On Saturday, June 20, 2020 at 3:08:42 PM UTC-4, David Finkel wrote: > > > > On Sat, Jun 20, 2020 at 11:03 AM T L > > 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{}) inte

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-20 Thread David Finkel
On Sat, Jun 20, 2020 at 4:53 PM Ian Lance Taylor wrote: > On Sat, Jun 20, 2020 at 12:08 PM David Finkel > wrote: > > > > 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

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-21 Thread Ian Lance Taylor
On Sat, Jun 20, 2020 at 8:08 PM T L wrote: > > > > On Saturday, June 20, 2020 at 3:08:42 PM UTC-4, David Finkel wrote: >> >> >> >> On Sat, Jun 20, 2020 at 11:03 AM T L wrote: >>> >>> >>> >>> On Saturday, June 20, 2020 at 10:21:56 AM UTC-4, Axel Wagner wrote: I would assume it's >>>

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-22 Thread T L
On Sunday, June 21, 2020 at 11:24:37 PM UTC-4, Ian Lance Taylor wrote: > > On Sat, Jun 20, 2020 at 8:08 PM T L > > wrote: > > > > > > > > On Saturday, June 20, 2020 at 3:08:42 PM UTC-4, David Finkel wrote: > >> > >> > >> > >> On Sat, Jun 20, 2020 at 11:03 AM T L wrote: > >>> > >>> >

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-22 Thread Ian Lance Taylor
On Mon, Jun 22, 2020 at 8:49 AM T L wrote: > > One example is the above Print function example. > Another example I current get is to iterate and print > all the key and values of a container in a current format. > There should be more examples with this need I think. I want to stress that we wan

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-23 Thread T L
On Monday, June 22, 2020 at 1:25:33 PM UTC-4, Ian Lance Taylor wrote: > > On Mon, Jun 22, 2020 at 8:49 AM T L > > wrote: > > > > One example is the above Print function example. > > Another example I current get is to iterate and print > > all the key and values of a container in a current f

Re: [go-nuts] Re: [generics] how to constraint a type must be a map or a slice?

2020-06-23 Thread Ian Lance Taylor
On Tue, Jun 23, 2020 at 8:34 AM T L wrote: > > On Monday, June 22, 2020 at 1:25:33 PM UTC-4, Ian Lance Taylor wrote: >> >> On Mon, Jun 22, 2020 at 8:49 AM T L wrote: >> > >> > One example is the above Print function example. >> > Another example I current get is to iterate and print >> > all the