[generics] A Channel of a Generic Structure

259 views
Skip to first unread message

teiva...@gmail.com

unread,
Jun 17, 2020, 3:44:57 PM6/17/20
to golang-nuts
Hello all,

I'm working on implementing generics on a big container projet.
So far, everything was good except for this very case:

type Foo(type T) struct {
   v int
}

func (_ *Foo(T)) bar() int {
   elem := <-make(chan Foo(T))
   
return elem.v
}

Foo being a generic structure, I have a method bar that has a Foo receiver (pointer or not, whatever).
This methods has to create a channel of Foo and return the first Foo.v value from the channel (of course this example is stupid but this a reduction from a real use-case).

I am unable to make this example to compile.

Do you guys have a solution?

Cheers

Harald Weidner

unread,
Jun 17, 2020, 5:37:28 PM6/17/20
to teiva...@gmail.com, golang-nuts
Hello,

> Here is the playground: https://go2goplay.golang.org/p/CVvUuNJVX-M
> I am unable to make this example to compile.

This one compiles:
https://go2goplay.golang.org/p/2rmaCymukdv

Regards,
Harald

Ian Lance Taylor

unread,
Jun 17, 2020, 6:39:02 PM6/17/20
to Harald Weidner, teiva...@gmail.com, golang-nuts
On Wed, Jun 17, 2020 at 2:37 PM Harald Weidner <hweidne...@gmx.net> wrote:
>
> > Here is the playground: https://go2goplay.golang.org/p/CVvUuNJVX-M
> > I am unable to make this example to compile.
>
> This one compiles:
> https://go2goplay.golang.org/p/2rmaCymukdv

Thanks. I'm not sure why those parentheses are needed. Filed
https://golang.org/issue/39670 to either fix the parser or update the
design draft.

Ian

teivah

unread,
Jun 18, 2020, 12:38:53 PM6/18/20
to golang-nuts
Cool, it does work, thanks!

Btw, thank you guys for your work. I know there are a lot of discussions going on but personally I'm very happy with generics so far. I'll keep working on my migration.
Reply all
Reply to author
Forward
0 new messages