Idea: Classifications

Daniel Keep daniel.keep+lists at gmail.com
Fri Jan 5 20:54:07 PST 2007


Xinok wrote:
> Basically, classifications are a set of aliases which can be used for comparison, during compile time or runtime. Classifications are different from tuples because tuples are a data type.
> 
> classify signed(byte, short, int, long);
> classify unsigned(ubyte, ushort, uint, ulong);
> classify intset(signed, unsigned); // You can use classifications in classifications
> 
> Classifications could also be used with variable types:
> classify prime(2, 3, 5, 7, 11, 13, 19, 23, 29);
> 
> What I meant about how they could be used for comparisons:
> Compile time:
> template temp(T : signed){ } // Classifications could be a good way to enable multiple specialization
> template temp(int V : prime){ }
> static if(is(V == prime))
> 
> Run time:
> int val;
> if(val == prime){ } // This would require an invisible loop to compare the variable to all the values in the classification
> if(val != prime){ }
> 
> Multiple Specialization:
> I believe you shouldn't have to declare a separate classification to enable multiple specialization.
> template temp(T : classify(int, long)){ }
> 
> 
> I believe my terminology is correct for 'classify' and 'classification':
> Classify : Arrange or order by classes or categories
> Classification : A group of people or things arranged by class or category

Why not just use tuples?

alias Tuple!(byte, short, int, long, cent) signed;
alias Tuple!(ubyte, ushort, uint, ulong, ucent) unsigned;
alias Tuple!(signed, unsigned) intset;

alias Tuple!(2, 3, 5, 7, 11, 13, 19, 23, 29) prime;

Compile time:
template temp(T : signed){ }
template temp(int V : prime){ }
static if(V in prime){ }

Run time:
int val;
if(val in prime){ }
if(val !in prime){ }

That way, we don't need a new keyword, or new syntax.  Just give tuples 
an 'in' operator that works at both compile and run time, and allow 
tuples to be used in specialisations.

	-- Daniel



More information about the Digitalmars-d mailing list