www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reflect on this function

reply "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
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?
Feb 20 2015
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Fri, 20 Feb 2015 22:32:53 +0000, Vlad Levenfeld wrote:

 I'd like to do something like this:
=20
     reflexive  transitive bool relation (T)(T a, T b)
    out (result) {
      mixin(property_verification!result);
    }
    body {
      ...
    }
=20
 which becomes
=20
    out (result) {
       // generated from  reflexive
      assert (result =3D=3D skip_contract!relation (b,a));
=20
      // generated from  transitive static typeof(result) c;
      if (result)
        assert (skip_contract!relation (b,c) =3D=3D
 skip_contract!relation (a,c));
      c =3D b;
    }
=20
 or something like that. I don't see a way to get exactly this,
 but does anyone have any thoughts on something similar?
can you go with `relationImpl` and mixin/template that generates=20 `relation` with contract then? something like: reflexive transitive bool relationImpl (T)(T a, T b) { ... } alias relation =3D buildWithContracts!relationImpl; then you can simply call `relationImpl` in your out section. sorry if i didn't understand what you want and just throws in some noise.=
Feb 20 2015
parent reply "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
On Friday, 20 February 2015 at 22:44:35 UTC, ketmar wrote:
 can you go with `relationImpl` and mixin/template that generates
 `relation` with contract then? something like:

    reflexive  transitive bool relationImpl (T)(T a, T b) { ... }
   alias relation = buildWithContracts!relationImpl;

 then you can simply call `relationImpl` in your out section.
Yeah, this looks pretty good. As much as I hate the pimpl idiom, having a one-liner alias right next to the definition should minimize the eye-bleeding. Thanks!
Feb 20 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Fri, 20 Feb 2015 22:51:21 +0000, Vlad Levenfeld wrote:

 On Friday, 20 February 2015 at 22:44:35 UTC, ketmar wrote:
 can you go with `relationImpl` and mixin/template that generates
 `relation` with contract then? something like:

    reflexive  transitive bool relationImpl (T)(T a, T b) { ... }
   alias relation =3D buildWithContracts!relationImpl;

 then you can simply call `relationImpl` in your out section.
=20 Yeah, this looks pretty good. As much as I hate the pimpl idiom, having a one-liner alias right next to the definition should minimize the eye-bleeding. Thanks!
and you can have an alias even before definition! ;-) this way it will=20 not lost.=
Feb 20 2015
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Feb 20 2015