Metadata
Title
Polymorphic Type Examples
Category
general
UUID
0da09bb39836432ea5a077c09dc16549
Source URL
https://cforall.uwaterloo.ca/features/polytype.shtml
Parent URL
https://cforall.uwaterloo.ca/features/
Crawl Time
2026-03-18T05:16:10+00:00
Rendered Raw Markdown
# Polymorphic Type Examples

**Source**: https://cforall.uwaterloo.ca/features/polytype.shtml
**Parent**: https://cforall.uwaterloo.ca/features/

```
#include <fstream.hfa>
#include <stdlib.hfa>
forall( 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 );
}
```