reflect on this function
    Ali Çehreli via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Fri Feb 20 15:24:53 PST 2015
    
    
  
On 02/20/2015 02:32 PM, Vlad Levenfeld wrote:
> I'd like to do something like this:
>
>    @reflexive @transitive bool relation (T)(T a, T b)
>    out (result) {
>      mixin(property_verification!result);
>    }
>    body {
>      ...
>    }
>
> which becomes
>
>    out (result) {
>       // generated from @reflexive
>      assert (result == skip_contract!relation (b,a));
>
>      // generated from @transitive
>      static typeof(result) c;
>      if (result)
>        assert (skip_contract!relation (b,c) == skip_contract!relation
> (a,c));
>      c = b;
>    }
>
> or something like that. I don't see a way to get exactly this, but does
> anyone have any thoughts on something similar?
Apparently, __FUNCTION__ is valid in an out block:
import std.stdio;
import std.string;
struct reflexive
{}
struct transitive
{}
string property_verification(alias var)(string func = __FUNCTION__)
{
     return format(
         `writefln("We are in %s; and the value of '%s' is '%%s'.", %s);`,
         func, var.stringof, var.stringof);
}
@reflexive @transitive bool relation (T)(T a, T b)
out (result) {
     mixin(property_verification!result);
} body {
     return false;
}
void main()
{
     int a, b;
     relation(a, b);
}
The output printed inside the out block:
We are in deneme.relation!int.relation; and the value of 'result' is 
'false'.
Ali
    
    
More information about the Digitalmars-d-learn
mailing list