Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 20, 2015 reflect on this function | ||||
---|---|---|---|---|
| ||||
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? |
February 20, 2015 Re: reflect on this function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld Attachments: | On Fri, 20 Feb 2015 22:32:53 +0000, 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? 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. sorry if i didn't understand what you want and just throws in some noise. |
February 20, 2015 Re: reflect on this function | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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!
|
February 20, 2015 Re: reflect on this function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld Attachments: | 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 = 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!
and you can have an alias even before definition! ;-) this way it will not lost.
|
February 20, 2015 Re: reflect on this function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | 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 |
Copyright © 1999-2021 by the D Language Foundation