Generic syntax idea

222 views
Skip to first unread message

mit...@gmail.com

unread,
Jun 19, 2020, 6:57:45 PM6/19/20
to golang-nuts
Hello, Ian, 

Would it be possible to have a generic type annotation syntax like this:

// Print prints the elements of any slice.
// Print has a type parameter T, and has a single (non-type)
// parameter s which is a slice of that type parameter.
func Print(type T)(s []T) {

==>

// Print prints the elements of any slice.
// Print has a type parameter T, and has a single (non-type)
// parameter s which is a slice of that type parameter.
func Print(s []T:type) {

This doesn't seem to clash with existing syntax, doesn't require a new pair of parenthesis, marks T as a generic type (and can have added constraint names right next to it, e.g: Print([]T:type constraint)

Or anything similar really, that keeps the generic type description in-line with regular types, e.g. could be a special sigil added to a type parameter as well (@T, $T, etc to mark it as generic, e.g.: Print([]$T constraint)

haven't thought this through by any means, but it strikes me as a cleaner option than a new set of parenthesis and duplication of the generic parameter names.

Does it make any sense? Don't recall seeing this in some other language...

Ian Lance Taylor

unread,
Jun 19, 2020, 7:09:00 PM6/19/20
to mit...@gmail.com, golang-nuts
Thanks for the note. It makes sense and offhand I don't see any
reason why it wouldn't work. But it doesn't seem very Go like. I
can't think of anything else in the language that works anything like
that.

Ian

Bubunyo Nyavor

unread,
Jun 20, 2020, 1:22:44 AM6/20/20
to golang-nuts
I think this is a much cleaner and more readable way to annotate generics in Go.  On the point of it not being a Go like, i don't think generics are Go like in the first place, but it looks like it is coming to stay. I think the go way is to make stuff easier to read and work with and if this doesn't present an issue with the compiler, it is worth at least exploring. 

Jake Montgomery

unread,
Jun 29, 2020, 12:28:14 PM6/29/20
to golang-nuts
I'm curious how you would propose rewriting:
func Foo(type T comparable, B interface{})(a T, b B, c T) {


bugpowder

unread,
Jun 29, 2020, 2:54:58 PM6/29/20
to golang-nuts
On Mon, Jun 29, 2020 at 7:28 PM Jake Montgomery <jake...@gmail.com> wrote:
I'm curious how you would propose rewriting:
func Foo(type T comparable, B interface{})(a T, b B, c T) {

You could just annotate each distinct type on first appeareance:

func Foo(a T:type comparable, b B:type interface{}, c T) {

 
--
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/0fbc45cf-1fe0-4910-adc1-9a6c77422566o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages