A proof-of-concept implementation of my generics proposal for Go

215 views
Skip to first unread message

Michal Strba

unread,
Jun 27, 2019, 10:29:03 AM6/27/19
to golang-nuts
Hey everybody!

A few weeks ago, I posted about my proposal for generics in Go 2. You can find it here.

Today, I finished a proof-of-concept implementation. It was a lot of fun and I learned a lot about the internals of Go.

The implementation is a program that takes a Go file that uses generics and translates it to a regular Go file that you can run.


I'm looking forwad to all your feedback!

Michal Štrba

Ian Davis

unread,
Jun 27, 2019, 12:52:25 PM6/27/19
to golan...@googlegroups.com
Syntactically this looks very good. I have read your gist but I'd like to see this as a proposal in the Go issue tracker.

Ian
--
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.
For more options, visit https://groups.google.com/d/optout.

Ben Hoyt

unread,
Jun 27, 2019, 10:13:08 PM6/27/19
to golang-nuts
I really, really like this. I liked the original proposal, but using the "type" keyword seems very natural. And I like the 80/20 constraints thing with eq/ord/num. Good work -- I hope this gets turned into an official proposal and debated.

-Ben

Randall O'Reilly

unread,
Jun 27, 2019, 11:14:09 PM6/27/19
to golang-nuts
I also like this proposal, and have a related proposal with a somewhat different syntax that is even simpler and is essentially identical to existing Go code, just with “Generic Native Types” (GNT). Here’s a couple of side-by-side comparisons:

///////////////////
// Min

Michal’s:

func Min(x, y type T ord) T {
if x < y {
return x
}
return y
}

GNT:

func Min(x, y number) type(x) {

}

///////////////////
// SyncMap

Michal’s:

type SyncMap(type K eq, type V) struct {
mu sync.Mutex
m map[K]V
}

func (sm *SyncMap(type K eq, type V)) Store(key K, value V) {
sm.mu.Lock()
sm.m[key] = value
sm.mu.Unlock()
}

GNT:

type SyncMap struct interface {
mu sync.RWMutex
m map // a generic map
}

func (m *SyncMap) Store(k key(m.Map), v elem(m.Map)) {

}

A struct interface is like an interface, specialized for struct -- any struct with the specified fields gets the generic methods, instantiated for the concrete type, e.g.,:

// MySyncMap instantiates the SyncMap struct interface...
type MySyncMap struct {
mu sync.RWMutex
m map[string]string
}

Here’s a significantly improved version of the proposal originally posted a few weeks ago:
https://gist.github.com/rcoreilly/bfbee2add03c76ada82810423d81e53d This proposal also specifies a complete set of *fixed* “contracts” that are isomorphic to the existing native types, so it might appeal to people who prefer more explicit semantics for the generic args, compared to more generic generics. Cheers,

- Randy
> --
> 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/199bad6c-ed62-44a6-960b-86503242537a%40googlegroups.com.

Michal Strba

unread,
Jun 30, 2019, 7:02:08 AM6/30/19
to golan...@googlegroups.com
I fixed the importing issues people were having with version of Go newer than 1.10.

If you haven't tried the generics tool yet and you're interested, now it should work for you!

It's a tool that takes a Go file which uses generics and translates it into a regular Go code that you can run. It also does full type-checking of the generic code.

Reply all
Reply to author
Forward
0 new messages