3 ms·
What you're after here is called parametric polymorphism (commonly known as "generics"). Go does not have it. Debates still rage on the ML about whether to have
by burntsushi 14y ago
What you're after here is called parametric polymorphism (commonly known as "generics"). Go does not have it. Debates still rage on the ML about whether to have it or not, but nobody has yet come up with an implementation makes most happy.
Go does have polymorphism though, in the form of subtype polymorphism. In Go, it is achieved using interfaces.
Subtype polymorphism typically has more programmer overhead than parametric polymorphism, so it is not used for simpler things like a generic map, fold, etc. However, it can be used to reduce code duplication in other cases, like sorting (among many other things).
- aaronblohowiak 14y agoI almost missed your ML pun. To the uninitiated, ML was the first language supporting parametric polymorphism around 1976. What is interesting about Go's subtype polymorphism is that it is structurally subtyped.. so A is a subtype of B if it fits the description of B, it does not need to explicitly declare that it "supports" or "implements" B.