Polymorphic Type Examples

#include <fstream.hfa>
#include <stdlib.hfa>
forall( otype T ) {								// distribution
	struct Stack { T * stack; int tp; };		// fixed array implementation
	void ?{}( Stack(T) & s, int sz ) with(s) { stack = alloc( sz ); tp = 0; }
	void ^?{}( Stack(T) & s ) with(s) { free( stack ); }
	T top( Stack(T) & s ) with(s) { return stack[tp]; }
	void push( Stack(T) & s, T val ) with(s) { stack[ tp++ ] = val; }
	T pop( Stack(T) & s ) with(s) { return stack[ --tp ]; }
}
int main() {
	Stack(int) s = { 10 };						// max depth
	for ( i; 3 ) push( s, i );
	for ( i; 3 ) sout | pop( s );
}