- Why we need generics?
- Why Go doesn't have generics (yet)?
- Walk through Golang Generics Draft Design!
- Does Go1 has a type inference?
- Parameterized types
- Contracts vs Interfaces
4. Why we need generics?
● Reduce the programmers overhead
● Increase code maintenance
● Increase type safety
● Reduce number of errors
● Make code faster
5. Why we need generics?
● Generic Data Structures
● Generic Algorithms
● Higher-order functions
● ….
9. “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
11. “(...) the design can be fully backward
compatible with Go1.”
Source: https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
12. “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
16. “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
27. “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
78. “(...) 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
Editor's Notes
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.
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
However, type parameters are not the same as non-type parameters, so although they appear in the parameters we want to distinguish them.
However, type parameters are not the same as non-type parameters, so although they appear in the parameters we want to distinguish them.
However, type parameters are not the same as non-type parameters, so although they appear in the parameters we want to distinguish them.
However, type parameters are not the same as non-type parameters, so although they appear in the parameters we want to distinguish them.
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.
We’ve dealt with generic functions
What about generic data structures?
Parameterized types
Parameterized types
Parameterized types
Parameterized types
We’ve dealt with generic functions
What about generic data structures?
Parameterized types
Parameterized types
Parameterized types
Parameterized types
Parameterized types
We’ve dealt with generic functions
What about generic data structures?
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.
Millions LOC
Hundreds of programers developing codebase
Millions LOC
Hundreds of programers developing codebase
Millions LOC
Hundreds of programers developing codebase
We’ve dealt with generic functions
What about generic data structures?
Parameterized types
We’ve dealt with generic functions
What about generic data structures?
We’ve dealt with generic functions
What about generic data structures?