Parametric Polymorphism
type variables


Task

Write a small example for a type declaration that is parametric over another type, together with a short bit of code (and its type signature) that uses it.



A good example is a container type, let's say a binary tree, together with some function that traverses the tree, say, a map-function that operates on every element of the tree.

This language feature only applies to statically-typed languages.




class Tree<T> {
    T value
    Tree<T> left
    Tree<T> right
 
    Tree(T value = null, Tree<T> left = null, Tree<T> right = null) {
        this.value = value
        this.left = left
        this.right = right
    }
 
    void replaceAll(T value) {
        this.value = value
        left?.replaceAll(value)
        right?.replaceAll(value)
    }
}