Polymorphic Function Examples

// Test resolvers ability to deal with many variables with the same name and to use the
// minimum number of casts necessary to disambiguate overloaded variable names.

#include <fstream.hfa>
#include <stdlib.hfa>

forall( T )
trait sumable {
	void ?{}( T &, zero_t );							// 0 literal constructor
	T ?+?( T, T );										// assortment of additions
	T ?+=?( T &, T );
	T ++?( T & );
	T ?++( T & );
}; // sumable

forall( T | sumable( T ) )								// use trait
T sum( size_t size, T a[] ) {
	T total = 0;										// initialize by 0 constructor
	for ( size_t i = 0; i < size; i += 1 )
		total += a[i];									// select appropriate +
	return total;
} // sum

int main( void ) {
	const int low = 5, High = 15, size = High - low;

	signed char s = 0, a[size], v = (char)low;
	for ( int i = 0; i < size; i += 1, v += 1hh ) {
		s += v;
		a[i] = v;
	} // for
	sout | "sum from" | low | "to" | High | "is"
		 | sum( size, (signed char *)a ) | ", check" | (signed char)s;

	unsigned char s = 0, a[size], v = low;
	for ( int i = 0; i < size; i += 1, v += 1hhu ) {
		s += (unsigned char)v;
		a[i] = (unsigned char)v;
	} // for
	sout | "sum from" | low | "to" | High | "is"
		 | sum( size, (unsigned char *)a ) | ", check" | (unsigned char)s;

	short int s = 0, a[size], v = low;
	for ( int i = 0; i < size; i += 1, v += 1h ) {
	 	s += (short int)v;
	 	a[i] = (short int)v;
	} // for
	sout | "sum from" | low | "to" | High | "is"
		 | sum( size, (short int *)a ) | ", check" | (short int)s;

	int s = 0, a[size], v = low;
	for ( int i = 0; i < size; i += 1, v += 1 ) {
		s += (int)v;
		a[i] = (int)v;
	} // for
	sout | "sum from" | low | "to" | High | "is"
		 | sum( size, (int *)a ) | ", check" | (int)s;

	float s = 0.0f, a[size], v = low / 10.0f;
	for ( int i = 0; i < size; i += 1, v += 0.1f ) {
		s += (float)v;
		a[i] = (float)v;
	} // for
	sout | "sum from" | low / 10.0f | "to" | High / 10.0f | "is"
		 | sum( size, (float *)a ) | ", check" | (float)s;

	double s = 0.0, a[size], v = low / 10.0;
	for ( int i = 0; i < size; i += 1, v += 0.1 ) {
		s += (double)v;
		a[i] = (double)v;
	} // for
	sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
		 | sum( size, (double *)a ) | ", check" | (double)s;

	struct S { int i, j; };
	void ?{}( S & s ) { s.[i, j] = 0; }
	void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; }
	void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; }
	void ?{}( S & s, zero_t ) { s.[i, j] = 0; }
	void ?{}( S & s, one_t ) { s.[i, j] = 1; }
	S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; }
	S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; }
	S ++?( S & t ) { t += (S){1}; return t; }
	S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }
	ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; }
	void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); nl( os ); }

	S s = (S){0}, a[size], v = { low, low };
	for ( int i = 0; i < size; i += 1, v += (S){1} ) {
		s += (S)v;
		a[i] = (S)v;
	} // for
	sout | "sum from" | low | "to" | High | "is"
		 | sum( size, (S *)a ) | ", check" | (S)s;

	forall( Impl | sumable( Impl ) )
	struct GS {
		Impl * x, * y;
	};
	GS(int) gs;
	gs.x = anew( size );								// create array storage for field
	s = 0; v = low;
	for ( int i = 0; i < size; i += 1, v += 1 ) {
		s += (int)v;
		gs.x[i] = (int)v;								// set field array in generic type
	} // for
	sout | "sum from" | low | "to" | High | "is"
		 | sum( size, gs.x ) | ", check" | (int)s;		// add field array in generic type
	delete( gs.x );
} // main