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.




$ include "seed7_05.s7i";
 
const func type: container (in type: elemType) is func
  result
    var type: container is void;
  begin
    container := array elemType;
 
    global
 
      const func container: map (in container: aContainer,
          inout elemType: aVariable, ref func elemType: aFunc) is func
        result
          var container: mapResult is container.value;
        begin
          for aVariable range aContainer do
            mapResult &:= aFunc;
          end for;
        end func;
 
    end global;
  end func;
 
const type: intContainer is container(integer);
var intContainer: container1 is [] (1, 2, 4, 6, 10, 12, 16, 18, 22);
var intContainer: container2 is 0 times 0;
 
const proc: main is func
  local
    var integer: num is 0;
  begin
    container2 := map(container1, num, num + 1);
    for num range container2 do
      write(num <& " ");
    end for;
    writeln;
  end func;