logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Generics-arent-ready-for-Go.md (6239B)


  1. ---
  2. date: 2019-02-18
  3. layout: post
  4. title: Generics aren't ready for Go
  5. tags: [language design, go]
  6. ---
  7. In the distance, a gradual roar begins to grow in volume. A dust cloud is
  8. visible over the horizon. As it nears, the shouts of the oncoming angry mob can
  9. be heard. Suddenly, it stops, and a brief silence ensues. Then the air is filled
  10. with the clackings of hundreds of keyboards, angrily typing the owner's opinion
  11. about generics and Go. The clans of Java, C#, Rust, C++, TypeScript, Haskell,
  12. and more - usually mortal enemies - have combined forces to fight in what may
  13. become one of the greatest flamewars of our time. And none of them read more
  14. than the title of this article before writing their comment.
  15. Have you ever seen someone write something to the effect of "I would use Go, but
  16. I need generics"? Perhaps we can infer from this that many of the people who are
  17. pining after generics in Go are not, in fact, Go users. Many of them are users
  18. of another programming language that *does* have generics, and they feel that
  19. generics are a good fit for this language, and therefore a good fit for any
  20. language. The inertia of "what I'm used to" comes to a violent stop when they
  21. try to use Go. People affected by this frustration interpret it as a problem
  22. with Go, that Go is missing some crucial feature - such as generics. But this
  23. lack of features is itself a feature, not a bug.
  24. Go strikes me as one of the most conservative programming languages available
  25. today. It's small and simple, and every detail is carefully thought out. There
  26. are very few dusty corners of Go - in large part because Go has fewer corners in
  27. general than most programming languages. This is a major factor in Go's success
  28. to date, in my opinion. Nearly all of Go's features are bulletproof, and in my
  29. opinion are among the best implementations of their concepts in our entire
  30. industry. Achieving this feat requires having *fewer* features in total.
  31. Contrast this to C++, which has too many footguns to count. You *could* write a
  32. book called "C++: the good parts", but consider that such a book about Go would
  33. just be a book about Go. There's little room for the bad parts in such a spartan
  34. language.
  35. So how should we innovate in Go? Consider the case of dependency management. Go
  36. 1.11 shipped with the first version of Go modules, which, in my opinion, is a
  37. game changer. I passionately hate `$GOPATH`, and I thought dep wasn't much
  38. better. dep's problem is that it took the dependency management ideas that other
  39. programming languages have been working with and brought the same ideas to Go.
  40. Instead, Go modules took the idea of dependency management and rethought it from
  41. first principles, then landed on a much more elegant solution that I think other
  42. programming languages will spend the next few years catching up with. I like to
  43. make an analogy to physics: dep is like [General Relativity][gr] or [the
  44. Standard Model][stdmodel], whereas Go modules are more like the [Grand Unified
  45. Theory][gut]. Go doesn't settle for anything less when adding features. It's
  46. not a language where liberal experimentation with imperfect ideas is desirable.
  47. [gr]: https://en.wikipedia.org/wiki/General_relativity
  48. [stdmodel]: https://en.wikipedia.org/wiki/Standard_Model
  49. [gut]: https://en.wikipedia.org/wiki/Grand_Unified_Theory
  50. I feel that this applies to generics. In my opinion, generics are an imperfect
  51. solution to an unsolved problem in computer science. None of the proposals I've
  52. seen (notably [contracts][proposal]) feel *right* yet. Some of this is a gut
  53. feeling, but there are tangible problems as well. For example, the space of
  54. problems they solve intersects with other Go features, which weakens the
  55. strength of both features. "Which solution do I use to this problem" is a
  56. question which different people will answer differently, and consequently their
  57. code at best won't agree on what "idiomatic" means and at worst will be simply
  58. incompatible. Another problem is that the proposal changes the meaning of
  59. idiomatic Go in the first place - suddenly huge swaths of the Go code, including
  60. the standard library, will become unidiomatic. One of Go's greatest strengths is
  61. that code written 5 years ago is still idiomatic. It's almost impossible to
  62. write unidiomatic Go code at all.
  63. [proposal]: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
  64. I used to sneer at the Go maintainers alongside everyone else whenever they'd
  65. punt on generics. With so many people pining after it, why haven't they seen
  66. sense yet? How can they know better than all of these people? My tune changed
  67. once I started to use Go more seriously, and now I admire their restraint. Part
  68. of this is an evolution of my values as a programmer in general: simplicity and
  69. elegance are now the principles I optimize for, even if it means certain classes
  70. of programs are simply not on the table. And I think Go should be comfortable
  71. not being suitable for writing certain classes of programs. I don't think
  72. programming languages should compete with each other in an attempt to become the
  73. perfect solution to every problem. This is impossible, and attempts will just
  74. create a messy kitchen sink that solves every problem poorly.
  75. <img
  76. src="https://sr.ht/TxC_.jpg"
  77. alt="Nina Tucker from Fullmetal Alchemist"
  78. width="320" />
  79. <p class="text-center">
  80. <small>fig. 1: the result of C++'s attempt to solve all problems</small>
  81. </p>
  82. The constraints imposed by the lack of generics (and other things Go lacks)
  83. breed creativity. If you're fighting Go's lack of generics trying to do
  84. something Your Way, you might want to step back and consider a solution to the
  85. problem which embraces the limitations of Go instead. Often when I do this the
  86. new solution is a much better design.
  87. So it's my hope that Go will hold out until the right solution presents itself,
  88. and it hasn't yet. Rushing into it to appease the unwashed masses is a bad idea.
  89. There are other good programming languages - use them! I personally use a wide
  90. variety of programming languages, and though I love Go dearly, it probably only
  91. comes in 3rd or 4th place in terms of how frequently it appears in my projects.
  92. It's *excellent* in its domain and doesn't need to awkwardly stumble into
  93. others.