On Tuesday, 4 June 2013 at 13:15:39 UTC, bearophile wrote:
An idea:

bearophile, you're a genius. I've been trying to think of a way to do this for the last week or so and you've got it.

usage:

struct small(T) if(T.sizeof < 9) {}

@CustomCheck!small
class RTTest {}


object.d additions:

        enum CustomCtCheckResult {
                fail, pass
        }
        template CustomCheck(alias C) {
                template CustomCheck(T) {
                        static if(__traits(compiles, C!T))
enum CustomCheck = CustomCtCheckResult.pass;
                        else
enum CustomCheck = CustomCtCheckResult.fail;
                }
        }

    bool doCustomChecks(T)() {
        if(__ctfe) {
             foreach(attr; __traits(getAttributes, T)) {
static if(is(typeof(attr!T) == CustomCtCheckResult)) { static assert(attr!T == CustomCtCheckResult.pass);
                 }
             }
             return true;
       }
   }

       template RTInfo(T) {
 /* other rtinfo stuff can remain */
                enum customChecksPassed = doCustomChecks!T;
        }




If we updated this to go right through the members too, we'd be really cooking.


object.d(755): Error: static assert (cast(CustomCtCheckResult)0 == cast(CustomCtCheckResult)1) is false object.d(774): instantiated from here: doCustomChecks!(RTTest)
minimal.d(168):        instantiated from here: RTInfo!(RTTest)


the last line helps find it. For members it will be trickier but there the CustomCheck template could take __FILE__ and __LINE__ as default arguments and just print them instead of static assert 0.

Reply via email to