[generics] Syntax feedback

399 views
Skip to first unread message

Rob Reid

unread,
Jun 27, 2020, 8:38:20 AM6/27/20
to golan...@googlegroups.com
Hi team,

I wanted to provide some feedback on the syntax used in the generics proposal.

When I first opened the Go 2 playground, it took me a while to realise that (type T) was a generic parameter and not a function parameter and that (s []T) was a function parameter and not a return parameter):

func Print(type T)(s []T) {
    for _, v := range s {
        fmt.Print(v)
    }
}


I think this would start to look more confusing when there are return values (either single or multiple):

func Something(type T)(s []T) (T, T)

I think another character would server to improve the readability of the Print function in this example and hopefully prevent the time required to grok a generic function to someone who has not seen one in Go before:

func Print<type T>(s []T) {
    for _, v := range s {
        fmt.Print(v)
    }
}

func Print[type T](s []T) {
    for _, v := range s {
        fmt.Print(v)
    }
}


Cheers,

Rob

Tyler Compton

unread,
Jun 27, 2020, 1:46:23 PM6/27/20
to Rob Reid, golang-nuts
Hi Rob,

This topic has been discussed many times on this list, so it's probably best to look at and post in one of those threads. Let me take this chance to collect as much information about this issue as I can in a single post.
Unfortunately, discoverability can sometimes be hard on mailing lists so I don't blame you for not seeing all these.

Responses on the subject:
Ian's justification for taking a wait-and-see approach to this syntax issue: https://groups.google.com/g/golang-nuts/c/Rumm5HFhg_Q

Threads:
Use "<>" before the name of the type being defined: https://groups.google.com/g/golang-nuts/c/TJWGbrx2o34
Put type parameters in the regular arguments list with a ":type" suffix: https://groups.google.com/g/golang-nuts/c/K7s-5MeXuzM
Use "<>", with some kind of additional syntax to make parsing easier: https://groups.google.com/g/golang-nuts/c/SaDkSQdgF9g


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CA%2Bcy9cUuJqOWWwi4m-MSp6gxKLGC4oFNn6y1zYJgN0Aac31sFg%40mail.gmail.com.

t hepudds

unread,
Jun 27, 2020, 3:45:47 PM6/27/20
to golang-nuts
Hi Tyler,

I just wanted to say a quick thank you for pulling those references together and your thoughtful response in this thread.

Hopefully the time you spent doing that will help additional gophers as the generics discussion continues on this list.

Regards,
thepudds

Ian Lance Taylor

unread,
Jun 27, 2020, 3:56:30 PM6/27/20
to Tyler Compton, Rob Reid, golang-nuts
On Sat, Jun 27, 2020 at 10:46 AM Tyler Compton <xav...@gmail.com> wrote:
>
> This topic has been discussed many times on this list, so it's probably best to look at and post in one of those threads. Let me take this chance to collect as much information about this issue as I can in a single post.
> Unfortunately, discoverability can sometimes be hard on mailing lists so I don't blame you for not seeing all these.
>
> Responses on the subject:
> Design draft talking about alternative syntax: https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#why-not-use-the-syntax-like-c_and-java
> Ian's justification for taking a wait-and-see approach to this syntax issue: https://groups.google.com/g/golang-nuts/c/Rumm5HFhg_Q
>
> Threads:
> Use "<>": https://groups.google.com/g/golang-nuts/c/LvkOBA2D_Bk
> Use "<>": https://groups.google.com/g/golang-nuts/c/B1Q1dsLa5rk
> Use "::" and other suggestions: https://groups.google.com/g/golang-nuts/c/b0GydCIn7T0
> Use "(<>)": https://groups.google.com/g/golang-nuts/c/tYwWeiMztiI
> Use "<>" before the name of the type being defined: https://groups.google.com/g/golang-nuts/c/TJWGbrx2o34
> Put type parameters in the regular arguments list with a ":type" suffix: https://groups.google.com/g/golang-nuts/c/K7s-5MeXuzM
> Use "<>", with some kind of additional syntax to make parsing easier: https://groups.google.com/g/golang-nuts/c/SaDkSQdgF9g
> Use "<>": https://groups.google.com/g/golang-nuts/c/coi7YS0KPgQ
> Use "<>": https://groups.google.com/g/golang-nuts/c/ydySSqZqi-0
> Use ":[]": https://groups.google.com/g/golang-nuts/c/zGQq_I5r2jg
> No specific suggestion: https://groups.google.com/g/golang-nuts/c/mY_36VU5ij8

Thanks for collecting that list.

Ian

Andrey T.

unread,
Jun 28, 2020, 4:09:40 PM6/28/20
to golang-nuts
Tyler,
May I humbly suggest https://groups.google.com/d/msg/golang-nuts/W3fSnH0w1G0/JbMkJrKICAAJ for an additional item for your list?

Thank you very much,
  Andrey

Tyler Compton

unread,
Jun 30, 2020, 1:29:04 PM6/30/20
to Andrey T., golang-nuts
Hi Andrey,

Unfortunately, I don't believe I can edit mailing list posts even on the Google Groups page. Sorry to have missed your post originally!

--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages