SlideShare a Scribd company logo
Go generics
What is this fuss about?
Go generics. what is this fuzz about?
Why we need generics?
Why we need generics?
● Reduce the programmers overhead
● Increase code maintenance
● Increase type safety
● Reduce number of errors
● Make code faster
Why we need generics?
● Generic Data Structures
● Generic Algorithms
● Higher-order functions
● ….
y:= make([]T, len(list))
for i, x := range list {
y[i] = fn(x)
}
seq.Map(list, fn)vs
parallel.Map(list, fn) vs seq.Map(list, fn)
Why Go doesn't have generics (yet)?
“Generics are a technical issue and are not a political one.
The Go team is not against generics per se, only against
doing things that are not well understood and/or don't work
well with Go.”
- Russ Cox
Source: https://news.ycombinator.com/item?id=9622417
Generics Draft Design!
“(...) the design can be fully backward
compatible with Go1.”
Source: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
“Among other languages
that support parametric polymorphism
this design is perhaps most similar to CLU or Ada.”
Source: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
Type parameters
func PrintTimes(s []interface{}, t int)
func PrintTimes(s []T, t int)
“In a language like Go, we expect
every identifier to be declared in some way.”
Source: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
func PrintTimes(s []T, t int)
func PrintTimes(s []T, t int)
func PrintTimes(s []T, t int)
func PrintTimes (s []T, t int)
func PrintTimes(type T)(s []T, t int)
PrintTimes(int)([]int{1, 2, 3}, 3)
PrintTimes(int)([]int{1, 2, 3}, 3)
PrintTimes(int)([]int{1, 2, 3}, 3)
PrintTimes ([]int{1, 2, 3}, 3)
PrintTimes ([]int{1, 2, 3}, 3)
“Type inference is a convenience feature.
Although we think it is an important feature, it does not add
any functionality to the design, only convenience in using it.”
Source: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
Does Go1 has a type inference?
var res *http.Response
var err error
res, err = http.Get("http://brainly.com/")
res, err := http.Get("http://brainly.com/")
How to declare more than one
type parameter?
func Print2(type T1, T2)(s1 []T1, s2 []T2)
Let’s look at map function
func Map(type T1, T2)(s []T1, f func(T1) T2) []T2 {
r := make([]T2, len(s))
for i, v := range s {
r[i] = f(v)
}
return r
}
func Map(type T1, T2)(s []T1, f func(T1) T2) []T2 {
r := make([]T2, len(s))
for i, v := range s {
r[i] = f(v)
}
return r
}
func Map(type T1, T2)(s []T1, f func(T1) T2) []T2 {
r := make([]T2, len(s))
for i, v := range s {
r[i] = f(v)
}
return r
}
s := []int{1, 2, 3}
slices.Map(s, func(i int) float64 {
return float64(i)
})
Parameterized types
type List struct {
next *List
val interface{}
}
type List(type T) struct {
next *List(T)
val T
}
func (l *List(T)) Val() T {
return l.val
}
l := list.List(int)
l.Append(1)
l.Tail().Val() == 1
Parameterized type aliases
type Ptr(type Target) = *Target
var i *int = &1 // Error!
j := 1
var i *int = &j // OK!
type Ptr(type Target) = *Target
var i *int = Ptr(int)(1)
type Ptr(type Target) = *Target
type PtrInt = Ptr(int)
var i *int = PtrInt(1)
Contracts
func PrintTimes(type T)(s []T, t int) {
for _, n := range s {
fmt.Print(n.String())
}
}
func PrintTimes(type T)(s []T, t int) {
for _, n := range s {
fmt.Print(n.String())
}
}
func PrintTimes(type T)(s []T, t int) {
for _, n := range s {
fmt.Print(n.Stringer())
}
}
PrintTimes(data, 3)
^^^^
Error: SomeStruct doesn’t have String() method,
Stringer() expected.
“(...) Go is designed to support programming at scale”
Source: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
contract stringer(x T) {
var s string = x.String()
}
func PrintTimes(type T stringer)(s []T, t int) {
for _, n := range s {
fmt.Print(n.Stringer())
}
}
func PrintTimes(type T stringer)(s []T, t int) {
^^^^^^^^^^
Error: PrintTimes does not follow contract
stringer. String() method call expected.
Contract vs Interface?
Source: https://twitter.com/rogpeppe/status/1036008488143646722
contract stringer(x T) {
var s string =
x.String()
}
type stringer interface {
String() string
}
vs
Source: https://gist.github.com/deanveloper/c495da6b9263b35f98b773e34bd41104
type Comparable interface {
operator[==]
operator[!=]
}
What contract can contract?
contract convert(t To, f From) {
To(f)
From(t)
f == f
}
contract strseq(x T) {
[]byte(x)
T([]byte{})
len(x)
}
contract G(n Node, e Edge) {
var _ []Edge = n.Edges()
var from, to Node = e.Nodes()
}
contract add1K(x T) {
x = 1000
x + x
}
contract iterable(x T) {
for T {}
}
Reflection
reflect.TypeOf(&List(int)).String()
=== "List(int)"
Efficiency
“do you want slow programmers, slow compilers and
bloated binaries, or slow execution times?”
- Russ Cox
Source: https://research.swtch.com/generic
“Only experience will show
what people expect in this area.”
Source: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
Omissions
Omissions
● No metaprogramming
● No covariance or contravariance.
● No operator methods (range, ==, m[k])
● No currying
● ...
What do you think about draft design?
vs
Thanks!
Gabriel Habryn
GitHub: github.com/widmogrod
Twitter: @widmogrod
Brainly Jobs: https://brainly.co/jobs.html
“(...) the type parameters are bounded not by a subtyping
relationship but by explicitly defined structural
constraints”
Source: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
Go generics. what is this fuzz about?

More Related Content

PDF
[Question Paper] Object Oriented Programming With C++ (Revised Course) [Janua...
PDF
ASP.NET With C# (Revised Syllabus) [QP / May - 2016]
PDF
ASP.NET With C# (Revised Syllabus) [QP / October - 2012]
PDF
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2014]
PDF
[Question Paper] Object Oriented Programming With C++ (Revised Course) [April...
DOCX
Conversion from infix to prefix using stack
PDF
[Question Paper] ASP.NET With C# (60:40 Pattern) [April / 2014]
[Question Paper] Object Oriented Programming With C++ (Revised Course) [Janua...
ASP.NET With C# (Revised Syllabus) [QP / May - 2016]
ASP.NET With C# (Revised Syllabus) [QP / October - 2012]
[Question Paper] Introduction To C++ Programming (Revised Course) [April / 2014]
[Question Paper] Object Oriented Programming With C++ (Revised Course) [April...
Conversion from infix to prefix using stack
[Question Paper] ASP.NET With C# (60:40 Pattern) [April / 2014]

What's hot (19)

PDF
[Question Paper] Fundamentals of Digital Computing (Revised Course) [April / ...
PDF
ASP.NET With C# (Revised Syllabus) [QP / October - 2016]
PDF
[Question Paper] Object Oriented Programming With C++ (Revised Course) [June ...
PDF
[Question Paper] Computer Graphics (Revised Course) [April / 2015]
PPTX
PPTX
Object Oriented Programming - Value Types & Reference Types
PPTX
C++ training day01
PDF
構文や語彙意味論の分析成果をプログラムとして具現化する言語 パターンマッチAPIの可能性
PPT
Clojure
PDF
[Question Paper] E-Commerce (Old Course) [September / 2013]
PDF
Pushover 2order (p delta effect) analysis force analogy method with force con...
PDF
C mcq practice test 4
PDF
Pushover analysis of frame by force analogy method with force control based o...
PDF
[Question Paper] ASP.NET With C# (60:40 Pattern) [October / 2013]
PDF
Geographic Information System (May – 2016) [75:25 Pattern | Question Paper]
PDF
Pythonとはなんなのか?
PPT
Module 2 topic 2 notes
PDF
[Question Paper] Computer Graphics (Old Course) [September / 2013]
PPTX
Visualizing Symbolic Execution with Bokeh
[Question Paper] Fundamentals of Digital Computing (Revised Course) [April / ...
ASP.NET With C# (Revised Syllabus) [QP / October - 2016]
[Question Paper] Object Oriented Programming With C++ (Revised Course) [June ...
[Question Paper] Computer Graphics (Revised Course) [April / 2015]
Object Oriented Programming - Value Types & Reference Types
C++ training day01
構文や語彙意味論の分析成果をプログラムとして具現化する言語 パターンマッチAPIの可能性
Clojure
[Question Paper] E-Commerce (Old Course) [September / 2013]
Pushover 2order (p delta effect) analysis force analogy method with force con...
C mcq practice test 4
Pushover analysis of frame by force analogy method with force control based o...
[Question Paper] ASP.NET With C# (60:40 Pattern) [October / 2013]
Geographic Information System (May – 2016) [75:25 Pattern | Question Paper]
Pythonとはなんなのか?
Module 2 topic 2 notes
[Question Paper] Computer Graphics (Old Course) [September / 2013]
Visualizing Symbolic Execution with Bokeh
Ad

Similar to Go generics. what is this fuzz about? (20)

PPT
go.ppt
PDF
To GO or not to GO
PPTX
Loom & Functional Graphs in Clojure @ LambdaConf 2015
PPTX
Go. Why it goes
PDF
Inroduction to golang
PPTX
[FOSS4G Seoul 2015] New Geoprocessing Toolbox in uDig Desktop GIS
PDF
Golang dot-testing-lite
PDF
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
PPT
Google Cluster Innards
PDF
Generative adversarial text to image synthesis
PDF
Declarative Semantics Definition - Term Rewriting
PDF
Golang
PPTX
Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT
DOCX
Company_X_Data_Analyst_Challenge
PDF
Go 1.10 Release Party - PDX Go
PDF
Coding in GO - GDG SL - NSBM
PDF
Swift for tensorflow
PDF
A gentle introduction to functional programming through music and clojure
PDF
Dynamics in graph analysis (PyData Carolinas 2016)
ODP
Clojure basics
go.ppt
To GO or not to GO
Loom & Functional Graphs in Clojure @ LambdaConf 2015
Go. Why it goes
Inroduction to golang
[FOSS4G Seoul 2015] New Geoprocessing Toolbox in uDig Desktop GIS
Golang dot-testing-lite
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Google Cluster Innards
Generative adversarial text to image synthesis
Declarative Semantics Definition - Term Rewriting
Golang
Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT
Company_X_Data_Analyst_Challenge
Go 1.10 Release Party - PDX Go
Coding in GO - GDG SL - NSBM
Swift for tensorflow
A gentle introduction to functional programming through music and clojure
Dynamics in graph analysis (PyData Carolinas 2016)
Clojure basics
Ad

More from Gabriel Habryn (6)

PDF
Type theory in practice
PPT
Interfejs konwersacyjny
ODP
SaturnAnalytic
KEY
Data grid w PHP
PPT
Cappuccino Framework
ODP
Jak żyć zdrowo
Type theory in practice
Interfejs konwersacyjny
SaturnAnalytic
Data grid w PHP
Cappuccino Framework
Jak żyć zdrowo

Recently uploaded (20)

PDF
Digital Strategies for Manufacturing Companies
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PPT
Introduction Database Management System for Course Database
PPTX
Introduction to Artificial Intelligence
PPTX
ai tools demonstartion for schools and inter college
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
L1 - Introduction to python Backend.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
Digital Strategies for Manufacturing Companies
Wondershare Filmora 15 Crack With Activation Key [2025
Operating system designcfffgfgggggggvggggggggg
Introduction Database Management System for Course Database
Introduction to Artificial Intelligence
ai tools demonstartion for schools and inter college
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Design an Analysis of Algorithms II-SECS-1021-03
CHAPTER 2 - PM Management and IT Context
VVF-Customer-Presentation2025-Ver1.9.pptx
L1 - Introduction to python Backend.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Nekopoi APK 2025 free lastest update
Navsoft: AI-Powered Business Solutions & Custom Software Development
Reimagine Home Health with the Power of Agentic AI​
Adobe Illustrator 28.6 Crack My Vision of Vector Design
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily

Go generics. what is this fuzz about?

Editor's Notes

  • #5: This adds overhead for the programmers and it can be error-prone to implement a single thing multiple times. Of course the re-implemented and copy-pasted code needs to be maintained. Similarly to code-generation this suffers from potential code-bloat. type-casts that could cause errors The reflection adds overhead and some type-casts may still be necessary.
  • #6: Don’t need to re-implement hard-to-get-right structures. Sets, Trees, Graphs,... You need to write code only to your specific needs and not the generic part. Sequential to concurrent with fewer changes in code Mutation of state by mistake in the input structs is much less likely to happen in functional code
  • #18: However, type parameters are not the same as non-type parameters, so although they appear in the parameters we want to distinguish them.
  • #19: However, type parameters are not the same as non-type parameters, so although they appear in the parameters we want to distinguish them.
  • #20: However, type parameters are not the same as non-type parameters, so although they appear in the parameters we want to distinguish them.
  • #21: However, type parameters are not the same as non-type parameters, so although they appear in the parameters we want to distinguish them.
  • #23: Since Print has a type parameter, when we call it we must pass a type argument. Type arguments are passed much like type parameters are declared: as a separate list of arguments. At the call site, the type keyword is not used.
  • #39: We’ve dealt with generic functions What about generic data structures?
  • #40: Parameterized types
  • #41: Parameterized types
  • #42: Parameterized types
  • #43: Parameterized types
  • #44: We’ve dealt with generic functions What about generic data structures?
  • #45: Parameterized types
  • #46: Parameterized types
  • #47: Parameterized types
  • #48: Parameterized types
  • #49: Parameterized types
  • #50: We’ve dealt with generic functions What about generic data structures?
  • #52: If the function is called with a type that does not have a String method, the error is reported at the point of the function call. These errors can be lengthy, as there may be several layers of generic function calls before the error occurs, all of which must be reported for complete clarity.
  • #55: Millions LOC Hundreds of programers developing codebase
  • #60: Millions LOC Hundreds of programers developing codebase
  • #62: Millions LOC Hundreds of programers developing codebase
  • #69: We’ve dealt with generic functions What about generic data structures?
  • #70: Parameterized types
  • #71: We’ve dealt with generic functions What about generic data structures?
  • #74: We’ve dealt with generic functions What about generic data structures?