Polymorphism

First-class polymorphism

Use `a instead of void *. In Cyclone you can store or pass polymorphic values without losing their polymorphism. Here’s an example:

/* First-class polymorphism example */
#include <list.h>
#include <stdio.h>

using List;

typedef `a (@id_t)<`a>(`a);
typedef int (@f_t)(int); 

`a id(`a x) { return x; }
`a id2(`a x) { return x; }
int mono(int x) {return x; }

list_t<id_t> l = NULL;

int main() {
  l = new List(id<>,l);    // <> prevents instantiation
  l = new List(id2<>,l);
  // l = new List(mono,l); // no good
  printf("%d\n",l->hd(3));
  printf("%s\n",l->hd("hello"));
  return 0;
}