June 04, 2013
On 2013-06-04 15:15, bearophile wrote:

> My original purposes for UDAs was to extend the type system, as
> you have essentially done here. But to do that well enough the
> attributes need to be more active.
>
> An idea:
>
> struct Small(T) if (T.sizeof <= 16) {}
>
> @Small struct Bar {
>        int x;
> }
>
>
> Here Small get instantiated with T == Bar, and its constraint
> performs the active compile-time test.

Absolutely. My idea for AST macros, which includes integration with UDA's, could possibly solve that.

https://dl.dropboxusercontent.com/u/18386187/ast_macros.html

See the bottom for attributes.

-- 
/Jacob Carlborg
June 04, 2013
On Tuesday, 4 June 2013 at 13:25:37 UTC, Jacob Carlborg wrote:
> On 2013-06-04 15:22, Namespace wrote:
>> Awesome! But I miss the filename and the line number where the error
>> happened.
>
> Hmm, I wonder if that's possible to get.

I do not know how. But with that it would be really incredible.
June 04, 2013
On 2013-06-04 15:22, Namespace wrote:
> Awesome! But I miss the filename and the line number where the error
> happened.

template RTInfo(T, string mod = __MODULE__, string file = __FILE__, size_t line = __LINE__)
{
    enum RTInfo = checkVirtual!(T, mod, file, line);
}

The above could work.

-- 
/Jacob Carlborg
June 04, 2013
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.
June 04, 2013
Though a downside here is you can't require a check attribute to be present on a class without modifying druntime. The class attribute could go down and check its members, but there's no way to error on an unchecked class without changing object.d, or at least a runtime check to loop through the modules and check it then.

It just checks ones that are there, and at that point, it isn't really that much different than just writing static assert(test.sizeof < 8); right there in the module.

so maybe not as awesome as i first thought, but still, this is an exciting technique and I'm sure there's more we can do with it.
June 04, 2013
On Tuesday, 4 June 2013 at 13:44:49 UTC, Jacob Carlborg wrote:
> On 2013-06-04 15:22, Namespace wrote:
>> Awesome! But I miss the filename and the line number where the error
>> happened.
>
> template RTInfo(T, string mod = __MODULE__, string file = __FILE__, size_t line = __LINE__)
> {
>     enum RTInfo = checkVirtual!(T, mod, file, line);
> }
>
> The above could work.

No chance without changing and rebuilding druntime?
June 04, 2013
On 2013-06-04 18:43, Namespace wrote:

> No chance without changing and rebuilding druntime?

Not that I can think of right now.

-- 
/Jacob Carlborg
1 2
Next ›   Last »