Generics syntax: declaring a generic type

68 views
Skip to first unread message

burak serdar

unread,
Jul 23, 2020, 12:36:44 AM7/23/20
to golang-nuts
This is a suggestion about the declaration of a generic type. There is
something unnatural in the syntax:

type SomeIntf(type T) interface {
...
}

type SomeStruct(type T) struct {
...
}

func SomeFunc(type T)(...) {...}


In all of the above, the names SomeIntf, SomeStruct, and SomeFunc are
being defined as generic types. However, all the regular types are
defined using the pattern:

"type" typeName typeDefinition

So why not:

type SomeIntf interface(T) {
...
}

type SomeStruct struct(T) {
...
}

func(T) SomeFunc(...) {...}

Ian Lance Taylor

unread,
Jul 23, 2020, 2:09:43 AM7/23/20
to burak serdar, golang-nuts
You are only looking at the declarations, not the uses. I think it is
a very nice feature that we can write

type SomeStruct(type T) Struct { ... {

var S SomeStruct(int)

The declaration and the use are parallel. It is obvious from the
placement of the type parameter how the type argument should be
written.

Ian
Reply all
Reply to author
Forward
0 new messages