www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - An Impressive Feature Without Doc

reply Wei Li <oldrev gmail.com> writes:
Just a moment ago, I met a cool feature but where is the specs for it?

template Floats(T) {
	static if(is(T : real)) {
		alias T Floats;
	}
}

T min(T=Floats)(T x, T y)  // Why we can write T=Floats instead of T=Floats!(T)
?
{
	return x < y ? x : y;
}

Regards
Nov 13 2007
next sibling parent Wei Li <oldrev gmail.com> writes:
Wei Li Wrote:

 Just a moment ago, I met a cool feature but where is the specs for it?
 
 template Floats(T) {
 	static if(is(T : real)) {
 		alias T Floats;
 	}
 }
 
 T min(T=Floats)(T x, T y)  // Why we can write T=Floats instead of
T=Floats!(T) ?
 {
 	return x < y ? x : y;
 }
 
 Regards
Sorry, my fault. I'm confused it with ':'. Regards
Nov 13 2007
prev sibling parent reply downs <default_357-line yahoo.de> writes:
Wei Li wrote:
 Just a moment ago, I met a cool feature but where is the specs for it?
 
 template Floats(T) {
 	static if(is(T : real)) {
 		alias T Floats;
 	}
 }
 
 T min(T=Floats)(T x, T y)  // Why we can write T=Floats instead of
T=Floats!(T) ?
 {
 	return x < y ? x : y;
 }
 
 Regards
This is one of D's template pitfalls; since min is never instantiated, it never actually checks if Floats is a valid thing to instantiate min with. (it's not; a template is not a type). You can see this by writing
 void main() {
   auto fp=&min!();
 }
which will make the compiler notice the error and barf. Thanks to KMD for pointing this out.
Nov 14 2007
parent Wei Li <oldrev gmail.com> writes:
downs Wrote:

 Wei Li wrote:
 Just a moment ago, I met a cool feature but where is the specs for it?
 
 template Floats(T) {
 	static if(is(T : real)) {
 		alias T Floats;
 	}
 }
 
 T min(T=Floats)(T x, T y)  // Why we can write T=Floats instead of
T=Floats!(T) ?
 {
 	return x < y ? x : y;
 }
 
 Regards
This is one of D's template pitfalls; since min is never instantiated, it never actually checks if Floats is a valid thing to instantiate min with. (it's not; a template is not a type). You can see this by writing
 void main() {
   auto fp=&min!();
 }
which will make the compiler notice the error and barf. Thanks to KMD for pointing this out.
I got it, thanks for your explain. Regards,
Nov 14 2007