www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - "static if" is awesome

reply Sean Kelly <sean f4.ca> writes:
It's how D simplifies everyday programming tasks that really sells me on 
this language.  'Atomic' in Ares now supports increment and decrement 
operations for numeric and pointer types only, and it does so without 
any weird template machinations or duplicated code:

struct Atomic(T)
{
     T load();
     void store( T newval );
     bool storeIf( T newval, T equalTo );

     static if( isValidNumericType!(T) )
     {
         T increment();
	T decrement();
     }
}

Try that in C++ :-P


Sean
Feb 23 2006
parent reply Don Clugston <dac nospam.com.au> writes:
Sean Kelly wrote:
 It's how D simplifies everyday programming tasks that really sells me on 
 this language.  'Atomic' in Ares now supports increment and decrement 
 operations for numeric and pointer types only, and it does so without 
 any weird template machinations or duplicated code:
 
 struct Atomic(T)
 {
     T load();
     void store( T newval );
     bool storeIf( T newval, T equalTo );
 
     static if( isValidNumericType!(T) )
     {
         T increment();
     T decrement();
     }
 }
 
 Try that in C++ :-P
 
 
 Sean

Fabulous. "static if" makes the easy stuff easy, and the hard stuff moderately easy <g> I'm tempted to report this line from http://www.digitalmars.com/d/overview.html as a documentation bug: ---------- Templates D templates offer a clean way to support generic programming while offering the power of partial specialization. ------- I think that since D has "static if", partial template specialisation is primarily for backwards compatibility with C++. It's a primitive, ugly technology. <g>
Feb 27 2006
parent Sean Kelly <sean f4.ca> writes:
Don Clugston wrote:
 I'm tempted to report this line from
 http://www.digitalmars.com/d/overview.html
 as a documentation bug:
 ----------
 Templates
 
 D templates offer a clean way to support generic programming while 
 offering the power of partial specialization.
 -------
 
 I think that since D has "static if", partial template specialisation is 
 primarily for backwards compatibility with C++. It's a primitive, ugly 
 technology. <g>

Yup :-) About the only time I use it any more is when I want to change the entire class implementation rather than just a few functions. Sean
Feb 27 2006