1

Hello everyone, I am fried fish.

Go1.18 beta1 has been released two days ago, two months before the production of Go1.18 is officially released, that is, generics will be officially available soon.

collecting some information about generics. I saw that in 2015, someone reported on Hacker News in "161c2ce782e1b2 Go 1.5 max procs default ". The reason Go does not support generics is "political"...

It still makes sense to see that is basically the same as the current contradiction point , so I will share it with you.

Netizens Tucao

Netizen @aikah believes that the Go team is unlikely to add generics to the language, which is obviously a political issue rather than a technical issue. The same is true for error handling.

Like many people, the netizen believes that Go does not strike the right balance between minimalism and functionality . Those who oppose generics favor compile-time type checking (always safe) in exchange for run-time type assertions (which may fail).

They refused to admit this fact. This is their argument against generics and will ultimately harm any potential growth of the language. They are basically going against their own interests.

Official reply

Russ Cox made a formal reply: I'm sorry, but not: Generic is a technical issue , not a political issue.

Go team and not opposed to the generic itself , but against doing what is not well understood or not well with Go's.

This is the core point of view and contradiction, which has continued from 2009 to the present.

Problems encountered

The Go team believes that in order to integrate the concept of generics into Go and work well with other parts of the system, some deep-seated technical problems must be solved, and we have no solution to these problems.

Regarding these issues, I wrote an article " The Generic Dilemma " on my blog a few years ago:

Even if you overcome the problem on that page, there are other problems. The next problem you will encounter is: "How to make programmers omit type comments in a useful and easy-to-interpret way".

That is, how to express generic type parameters more humanely and easily.

Generic example

For example, C++ allows you to write make_pair(1, "foo") instead of make_pair<int, string>(1, "foo") .

In order to achieve this effect, inferring the logic behind the comments requires several pages of specifications. This is not a particularly easy-to-understand programming model. When things go wrong, the compiler cannot easily explain them.

After this, there must be more new problems in it.

Communicate with experts

The Go team talked to some real Java generics experts, and each of them said roughly the same thing: Be very careful, it's not as easy as it seems, and you will be trapped by all the mistakes you make.

As a Java demonstration, you can browse most of the contents of Java Generics FAQs-Frequently Asked Questions

See how long it takes you to start thinking "Is this really the best way?".

There are many problems encountered in the process of generics, such as " How do I decrypt Enum<E extends Enum<E>> ":

For this reason, the Go team is very cautious in promoting generics.

Admit shortcomings

Go team made it clear that recognize this fact: no generics are certain disadvantages .

You either use interface{} and give up compile-time checking, or write a code generator to complicate your build process.

The generics implemented in existing languages also have clear shortcomings, and today’s uncompromising has a very big benefit: it makes it easier for adopt a better solution .

Summarize

Today, I shared with you various controversies and discussions on Go generics in foreign communities in the past. In fact, the core point of generics is very clear: "The Go team does not oppose generics itself".

I haven't been able to make generics because of many concerns. Generics must be made in multiple parts with Go, and many deep-seated problems must be solved, as well as the readability of type parameters. So it has been delayed until now.

Going back to the present of 2022, the predictions are correct. The community is talking about multiple related components after generics, as well as generic readability and structure...

Obviously, the generic is a double-edged sword? What do you think of .

If you have any questions, welcome feedback and communication in the comment area. The best relationship between . Your likes is the biggest motivation for the creation of fried fish

Article continually updated, you can head into the micro-channel search [fried] read the article GitHub github.com/eddycjy/blog already been included, Go language learning can be seen Go Learning Map and Directions , welcome Star urge more.

煎鱼
8.4k 声望12.8k 粉丝