by rogpeppe1 · 11y ago · 0 comments
For example, bufio.NewReader would probably, in a language with generics, be phrased as a type with respect to a type parameter. Something like (in bastard Haskell)
class Reader a where
Read :: a -> [Char]
newReader :: (Reader a) => a -> BufioReader a
In Go, it's: package bufio
func NewReader(r io.Reader) *Reader
The interface argument is implicitly a type parameter here.This subset of generics covers a wide range of software components, even if it doesn't include the usual container types.
With Go, polymorphism is done with interfaces. Composition is done with embedded fields (and the very convenient method promotion).