www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Idea: Classifications

reply Xinok <xnknet gmail.com> writes:
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
Jan 05 2007
next sibling parent reply Witold Baryluk <baryluk mpi.int.pl> writes:
Dnia Fri, 05 Jan 2007 14:38:00 -0500
Xinok <xnknet gmail.com> napisa=B3/a:

 Basically, classifications are a set of aliases which can be used for
 comparison, during compile time or runtime.

 What I meant about how they could be used for comparisons:
 Compile time:
 template temp(T : signed){ } // Classifications could be a good way

template isSigned(T) { static if (is(T =3D=3D int)) { ; }=20 else static if (is(T =3D=3D long)) { ; }=20 else static assert(false, "inproper type"); } template temp(T) { mixin Check!(T); ... } longer, but not so bad, once created. Mayby someone will post here better implementation, probably using TypeTuples.
=20
 Run time:
 int val;
 if(val =3D=3D prime){ }

You can use operator overloading. Or if you realy need your kind of classification, i suggest noted used operator like if (classify(val =3D=3D prime)) or if (val ofclass prime) you can also create similar tamplete, for value classyfication: use static table, and mixin foreach, or unrool it useing static foreach, or use better algoritm. Any other exmples, that eventually will be hard to implement in other way? --=20 Witold Baryluk
Jan 05 2007
parent Xinok <xnknet gmail.com> writes:
-- Why not use tuples?
Tuples weren't meant to be used like classifications. Classifications have one
main purpose - To be used for comparison. Tuples are more of a data type, a
struct almost, they weren't meant to be used in this style.


-- Overloading an Operator
I'm a big fan of convenience, which is the only reason why I posted this idea.
IMO, defining a class and overloading the operator is too much work for such a
simple concept. It would be easier just to use a function:

bool isprime(int arg){
	static int[] primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
	foreach(prime; primes) if(arg == prime) return true;
	return false;
}

And before you say "Then why not use a function?", Simple: You can't use a
function at compile time, only during runtime.


-- Using Static If for Multiple Specialization
While it's unlikely to be an issue, when using static if's in templates, it's
possible that they can hide declarations if you implicitly instantiate the
template.

template func(T){
	static if(is(T == int)) void func(T arg){ } // 'specialized' func
	else void func(T arg){ } // unspecialized func
}

func(35); // Error
func!(int)(35); // OK


-- 'Traits'
A trait is more like an attribute or property. A classification is more like a
group.
Also, class != classification
Jan 05 2007
prev sibling next sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
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

It isn't a bad idea. Although maybe a better term would be "traits" (classification isn't bad, just potentially ambiguous with OOP classes). trait signed (byte, short, int, long); template temp (T : signed) {} template tempmulti (T : trait(int, long)) {} With the same rules as Tuples: either types or data, not both. There may well be sensible ways of making this happen right now with Tuples though, given a little more working out. (Not right now as in with the current compiler, but right now as in hooking existing features to each other.) -- Chris Nicholson-Sauls
Jan 05 2007
parent reply BCS <nothing pathlink.com> writes:
Chris Nicholson-Sauls wrote:
 
 With the same rules as Tuples: either types or data, not both.

? Tuples can do mixed lists of whatever. from: http://www.digitalmars.com/d/tuple.html template Tuple(E...){ alias E Tuple; } [...] Tuple!(int, 8) // create a tuple of a type and an expression The implied restriction would be good for classifications, but I didn't think it apples to tuples.
Jan 05 2007
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
BCS wrote:
 Chris Nicholson-Sauls wrote:
 With the same rules as Tuples: either types or data, not both.

? Tuples can do mixed lists of whatever. from: http://www.digitalmars.com/d/tuple.html template Tuple(E...){ alias E Tuple; } [...] Tuple!(int, 8) // create a tuple of a type and an expression The implied restriction would be good for classifications, but I didn't think it apples to tuples.

My mistake then. Actually that makes Tuples even more useful, so it makes sense. -- Chris Nicholson-Sauls
Jan 05 2007
prev sibling parent Daniel Keep <daniel.keep+lists gmail.com> writes:
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
Jan 05 2007