Update: On 31st July, Ian Lance Taylor posted in detail explaining the benefits and costs of including generics in Go. He also briefly discussed the draft design to convey how generics is going to be added to the Go language. Taylor says “Our goal is to arrive at a design that makes it possible to write the kinds of generic code, without making the language too complex to use or making it not feel like Go anymore.”
Check out the Goland blog for more details.
On 26th July at GopherCon 2019, Ian Lance Taylor introduced generics codes in Go. He briefly explained the need, implementation and benefits from generics for the Go language. Next, Taylor reviewed the Go contract design draft which included addition of optional type parameters to types and functions.
I'm glad to meet and talk to him in person. I'm appreciate his gentleness and kind answer to my silly questions. Definitely he is in the frontier along with his smart companies. That is one of the reasons why I trust the future of Go.
— Yoshi Yamaguchi 🇯🇵 (@ymotongpoo) July 27, 2019
Great talk about generics by Ian Lance Taylor
#gophercon pic.twitter.com/NeoxacCBsE
— Elena Morozova (@lelenanam) July 26, 2019
Taylor also proposed guidelines for implementing generic design in Go.
Goals and constraints for generics in Go. #golang #GopherCon pic.twitter.com/tCv6dHTqhs
— Aditya Mukerjee, the Otterrific 🏳️🌈 (@chimeracoder) July 26, 2019
In all the three years of Go surveys, lack of generics has been listed as one of the three highest priorities for fixing the Go language.
Taylor defines generic as “Generic programming which enables the representation of functions and data structures in a generic form, with types factored out.”
Generic code is written using types, which are specified later. An unspecified type is called as type parameter. A type parameter offers support only when permitted by contracts.
A generic code imparts strong basis for sharing codes and building programs. It can be compiled using an interface-based approach which optimizes time as the package is compiled only once. If a generic code is compiled multiple times, it can carry compile time cost.
Some of the many functions that can be written generically in Go include –
Image Source: Source graph
Go already supports two generic data structures which are built using Slice and Map languages. Go requires data structures to be written only once and then reused after putting it in a package.
The contract draft design states that since Go is designed to support programming, a clear contract should be maintained between a generic code and a calling code. With the new changes, users may find the language more complex. However, the Go team expects users to not write generic code themselves, instead use packages that are written by others using generic code.
Developers are very happy that the Go generics proposal is simple to understand and enables users to depend on the already written generic packages. This will save them time as users need not rewrite type specific functions in Go.
It's pretty cool to see how the Go team have absorbed feedback and worked to simplify the generics proposal. Love the emphasis on making generic code easy to use, (extra complexity handled by the writer of generic code not the user). #golang https://t.co/JVi1XRbGIs
— Liz Rice (@lizrice) July 26, 2019
New Go generics proposal looks nice. If generic embedded structs work, it will be possible to write mixins: logic and state can then be decorated around any compatible type, without code duplication, without code gen, during compile time. https://t.co/jI2j85Dzhn
— protolambda 🍍 (@protolambda) July 28, 2019
It’s not hard rewriting lots of type specific functions in Go, but it would be easier if we could write them once, generically. Ian Lance Taylor is giving a measured, thorough explanation for why generics might be a good idea in Go pic.twitter.com/K3RA3E2EC9
— Aaron Schlesinger (@arschles) July 26, 2019
IMHO this is all going in the right way.
I really like the current draft / proposal.
Avoiding other languages pitfalls is exactly what I would expect from the Go lang.
Keep up the great work!— Yvan da Silva (@YvanDaSilva) July 28, 2019
First impression on Ian Lance Taylor update on generics: the new contracts look muc simpler and feel great to use.
Generics on Go are starting to look good.#gophercon— Mickaël Rémond (@mickael) July 26, 2019
Users have also admired the new contract design draft by the Go team.
I was skeptical about generics in Go, but the proposed design we saw this morning looks pretty good and feels Go-like to me. #gophercon
— Tatsuhiko Miyagawa (@miyagawa) July 26, 2019
The new contract syntax feels less prone to confusion. Being able to specify fields would be nice, but not essential. Really like that types aren't boxed.
— T Colgate-McFarlane (@t_colgate) July 28, 2019
Really happy with the new contracts design!
Waaaaay simpler than it used to be#golang #gophercon https://t.co/yQuI4y3s0C
— francesc (@francesc) July 26, 2019
Head over to the Google proposal page for more details on the new contract draft design.
Read More
Ian Lance Taylor, Golang team member, adds another perspective to Go being Google’s language