Generics / Type Parameters

(not yet implemented)

Type Parameters in smalldragon allow for writing code that can handle any Data Type, in a typesafe manner.

Generic Struct Declaration

struct List <?T0> {
	[?T0] arr;
	uint capacity;
	uint count;
}

Generic Function Declaration

fn list_add(List <?T0> list, ?T0 elem) ~> bool {
	list.arr[list.count] = elem;
	list.count++;
	return true;
}

Usage of the generic List

fn main() ~> int {

	List<int> list = malloc(8*3);
	list.cap   = 10;
	list.count = 0;
	list.arr   = malloc(8*list.cap);
	
	list_add(list, 3);
	
	list_dtor(list);

	return 0;
}