Red Code, Green Code (nwcpp video)

Pragma ericanderton at yahoo.removeme.com
Wed May 9 08:18:03 PDT 2007


janderson wrote:
> Pragma wrote:
> 
> Maybe something like C# user-definable attributes would help. (I'm sure 
> someone has asked for this before).

I'm pretty sure I have on a few occasions. ;)

One way I can see this working is to use a custom pragma() - I think I managed to talk Gregor into trying it out in 
Rodin, at some point or another.  The suggestion isn't a personal bias, it's just that they're pretty much the ideal 
construct in D for supporting attributes.

struct ExceptionSafe{
   static ExceptionSafe opCall(){
     ExceptionSafe_this;
     return _this;
   }
}

// attach the ExceptionSafe attribute to the function f()
pragma(attr,ExceptionSafe()) void f(){
   /* do something */
}

But there are a few problems with using reflection to accomplish constraint checks.  The most obvious of which is that 
we're now checking things at runtime rather than at compile-time; so you have to run a full test suite in order to make 
sure you coded things up correctly.  Also, without being able to get the attributes for the current function, or even 
the calling function, there's no way to check the constraints without wrapping the call with a template.

The mess from using templates for this comes from the fact that the constraints can be thrown into a list in any order, 
so it defies the built-in specialization and overloading mechanisms provided by D.  If there were some way to specialize 
function declarations and calls, via an unordered property set (or by compile-time attributes), then it would be very 
easy to do.

-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list