Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get around Go not having parametric polymorphism?

Tags:

People also ask

How do you achieve polymorphism in GoLang?

In GoLang, polymorphism is achieved mainly using interfaces. A type implementing a function defined in interface becomes the type defined as an interface. This is the property that makes polymorphism achievable in Go.

Is there polymorphism in GoLang?

Golang is a light-Object Oriented language and supports polymorphism through interfaces only.

What is the use of parametric polymorphism?

Parametric Polymorphism allows the creation of generic functions with generic data structures in order to secure staticity and handle values the same without depending on their type. Using parametric polymorphism, you can apply generic functions to constructors, which allows for faster calculation and consistent data.

What is implicit parametric polymorphism?

Implicit polymorphism can be considered as an abbreviated form of explicit polymorphism, where the type parameters and applications have been omitted and must be rediscovered by the language processor. Omitting type parameters leaves some type-denoting identifiers unbound; and these are precisely the type variables.


I'm a Go newcomer, but I have read that Go regulars do not miss parametric polymorphism. Every time I try to learn a new language I use the L99 list of problems to get some practice.

Even if I try to write something as trivial as the first problem (which in Go would be a single statement, taking the last element of a slice), how would I write this as a function that takes a slice of any type and (using that single statement I referenced above) returns the last element of that slice?

I figured even though the language does not have parametric polymorphism there must be some idiomatic 'Go' way of doing this in order for Go regulars to claim they dont miss parametric polymorphism. Otherwise, if the example were more complex than just the last element of a list for instance, you would need a function to perform your task for every type.

What am I missing?