Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go 2: A compatible generics syntax #36457

Closed
kulics opened this issue Jan 8, 2020 · 2 comments
Closed

proposal: Go 2: A compatible generics syntax #36457

kulics opened this issue Jan 8, 2020 · 2 comments
Labels
FrozenDueToAge generics Issue is related to generics LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@kulics
Copy link

kulics commented Jan 8, 2020

Go 2 Generic Design

Recently I explored a new generic syntax in the Feel language that I designed, because Feel borrowed a lot of grammar from Go, so this Generic syntax may also have some reference value for Go.

The identifier<T> problem is that it conflicts with comparison operators and also bit operators, so I don't agree with this design.

Scala's identifier[T] has a better look and feel than the previous design, but after resolving the above conflict, it has a new conflict with the index design identifier[index].
For this reason, the index design of Scala has been changed to identifier(index). This does not work well for languages ​​that already use [] as an index.

In Go's draft, it was declared that generics use (type T), which will not cause conflicts, because type is a keyword, but the compiler still needs more judgment when it is called to resolve theidentifier(type)(params). Although it is better than the above solutions, it still does not satisfy me.

By chance, I remembered the special design of method invocation in OC, which gave me inspiration for a new design.

What if we put the identifier and the generic as a whole and put them in [] together?
We can get the [identifier T]. This design does not conflict with the index, because it must have at least two elements, separated by spaces.
When there are multiple generics, we can write [identifier T V] like this, and it will not conflict with the existing design.

Substituting this design into Go, we can get the following example.
E.g.

type [Item T] struct {
    Value T
}

func (it [Item T]) Print() {
    println(it.Value)
}

func [TestGenerics T V]() {
    var a = [Item T]{}
    a.Print()
    var b = [Item V]{}
    b.Print()
}

func main() {
    [TestGenerics int string]()
}

This looks very clear.

Another benefit of using [] is that it has some inheritance from Go's original Slice and Map design, and will not cause a sense of fragmentation.

[]int  ->  [slice int]

map[string]int  ->  [map string int]

We can make a more complicated example

var a map[int][]map[string]map[string][]string

var b [map int [slice [map string [map string [slice string]]]]]

This example still maintains a relatively clear effect, and at the same time has a small impact on compilation.

I have implemented and tested this design in Feel and it works well.

I think this design has a certain reference value and may be worthy of discussion.

@gopherbot gopherbot added this to the Proposal milestone Jan 8, 2020
@ianlancetaylor ianlancetaylor added generics Issue is related to generics v2 An incompatible library change LanguageChange Suggested changes to the Go language labels Jan 8, 2020
@ianlancetaylor
Copy link
Contributor

Thanks. Right now we are focusing on semantics rather than syntax, but we'll keep this issue open.

@bradfitz bradfitz changed the title proposal: Go 2: A compatible generic design proposal: Go 2: A compatible generics syntax Jan 8, 2020
@ianlancetaylor
Copy link
Contributor

Upon discussion with Go 2 proposal review committee, we would like to fold this issue into the general generics issue #15292. Please also feel free to add a link to this issue to https://github.com/golang/go/wiki/Go2GenericsFeedback. We don't think it will be helpful to keep multiple generics syntax issues open simultaneously, spreading the discussion to too many different places.

-- for @golang/proposal-review

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge generics Issue is related to generics LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

3 participants