assert() vs. enforce(), invariant() vs. ... ?
Artur Skawina
art.08.09 at gmail.com
Thu Aug 29 08:56:19 PDT 2013
On 08/29/13 16:55, Joseph Rushton Wakeling wrote:
> On 29/08/13 16:21, Artur Skawina wrote:
>> In order to handle ref-returns it would have to be more like
>>
>> struct EnforceWrapper(T) {
>> T t;
>>
>> auto ref opDispatch(string M, Args...)(Args args) {
>> {/*before code*/}
>> scope (exit) {/*after code*/}
>> return mixin("t." ~ M ~ "(args)");
>> }
>> }
>>
>> and need another two overloads for property/field getters and
>> setters. Handling ref-args would add more complexity, but in
>> many cases ignoring them or using auto-ref is enough.
>
> Interesting. When it comes down to it, it looks like what you're proposing is an extended version of Proxy, no .... ?
Maybe, no idea what Proxy does.
'opDispatch' will work for simple cases, and in situations like
the one you described, where you just want a bit of sugar for a
/local/ always-on invariant, have full control over the wrapped
type, and want to eliminate the boilerplate that would otherwise
be required.
A more generic solution would be something like:
struct EnforceWrapper(T) {
T t;
mixin wrapMembers!t;
void _before(string M, A...)() {...}
void _after(string M, A...)() {...}
}
where the 'wrapMembers` mixin-template examines 't' and generates
the necessary wrappers. That way overload sets will keep working, etc.
artur
More information about the Digitalmars-d
mailing list