www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Enforcing checks for return code

reply Chris Katko <ckatko gmail.com> writes:
Hello. I'm almost brand-new to the D language and still absorbing 
things.

I'm wondering if it's possible to fire off a compile-time (or 
worst case, a run-time) warning or error if a function is called, 
but the return value is not checked.

I'm not trying to enforce whether someone actually deciphers the 
value's meaning correctly. I just want to enforce that somewhere, 
a variable or expression is receiving the return value of a 
particular function.

Any ideas?

I imagine I could use a compiler flag to warn, but that's a 
global setting. I'm looking more for a specified subset of 
functions.
Feb 17 2016
next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, February 18, 2016 07:21:05 Chris Katko via Digitalmars-d-learn
wrote:
 Hello. I'm almost brand-new to the D language and still absorbing
 things.

 I'm wondering if it's possible to fire off a compile-time (or
 worst case, a run-time) warning or error if a function is called,
 but the return value is not checked.

 I'm not trying to enforce whether someone actually deciphers the
 value's meaning correctly. I just want to enforce that somewhere,
 a variable or expression is receiving the return value of a
 particular function.

 Any ideas?

 I imagine I could use a compiler flag to warn, but that's a
 global setting. I'm looking more for a specified subset of
 functions.
D has no such feature, and very little D code is going to use return codes. D has exceptions, and throwing an exception is the normal way to indicate that there was a problem. You certainly _can_ choose to use error codes if you'd like, but it's not normal practice, and the language does nothing extra to support it. - Jonathan M Davis
Feb 18 2016
prev sibling next sibling parent tsbockman <thomas.bockman gmail.com> writes:
On Thursday, 18 February 2016 at 07:21:05 UTC, Chris Katko wrote:
 Hello. I'm almost brand-new to the D language and still 
 absorbing things.

 I'm wondering if it's possible to fire off a compile-time (or 
 worst case, a run-time) warning or error if a function is 
 called, but the return value is not checked.

 I'm not trying to enforce whether someone actually deciphers 
 the value's meaning correctly. I just want to enforce that 
 somewhere, a variable or expression is receiving the return 
 value of a particular function.

 Any ideas?

 I imagine I could use a compiler flag to warn, but that's a 
 global setting. I'm looking more for a specified subset of 
 functions.
Off hand, I see two reasonable ways to do this: 1) Instead of actually returning the value, require the caller to pass the destination variable as a `ref` or `out` parameter. Disadvantages: This could be a bit slower than using actual return values, and it would change the syntax. 2) Wrap the return value in a struct like this: struct MustUse(T) { pure: nothrow: private: T payload; bool used; public: property ref T use() nogc { used = true; return payload; } alias use this; this(T payload) nogc { this.payload = payload; this.used = false; } ~this() { if(!used) throw new Error(T.stringof ~ " return value was not used."); } } // For convenience, use this function to infer T from the type of paylaod. MustUse!T mustUse(T)(T payload) { return MustUse!T(payload); } Example on DPaste: http://dpaste.dzfl.pl/8ba6ebf05f32 With inlining and optimizations, even the above generic implementation might be faster than passing by ref/out. A specialization for non-null pointer values could almost certainly be faster. Disadvantages: On the other hand, it might be slower. The only way to know for sure, is to write some non-trivial examples and benchmark. Regardless, the fact that it signals the error at runtime instead of compile time could be annoying.
Feb 18 2016
prev sibling parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Thursday, 18 February 2016 at 07:21:05 UTC, Chris Katko wrote:
 Hello. I'm almost brand-new to the D language and still 
 absorbing things.

 I'm wondering if it's possible to fire off a compile-time (or 
 worst case, a run-time) warning or error if a function is 
 called, but the return value is not checked.

 I'm not trying to enforce whether someone actually deciphers 
 the value's meaning correctly. I just want to enforce that 
 somewhere, a variable or expression is receiving the return 
 value of a particular function.

 Any ideas?
As Jonathan said, there's no such built-in feature, and exception are preferred over return codes. However, you can implement such a check at run time: struct ForceCheck(T) { private T payload; private bool checked = false; disable this(); disable this(this); this()(auto ref T payload) { this.payload = payload; } ref T get() { this.checked = true; return payload; } alias get this; void ignore() { this.checked = true; } ~this() { assert(this.checked, "you forgot to check the return value"); } } auto forceCheck(T)(auto ref T value) { return ForceCheck!T(value); } auto foo() { return forceCheck(42); } void main() { { auto a = foo(); if(a != 42) { } // stored values } { if(foo() != 42) { } // direct access } { foo().ignore(); // explicitly ignore return value } { auto b = foo(); // this one asserts foo(); // as does this one } } I guess it's reasonably performant; it could be optimized further by only adding the `checked` member if assertions are enabled (using `version(assert)`).
Feb 18 2016
parent Chris Katko <ckatko gmail.com> writes:
On Thursday, 18 February 2016 at 10:46:03 UTC, Marc Sch├╝tz wrote:
 On Thursday, 18 February 2016 at 07:21:05 UTC, Chris Katko 
 wrote:
[...]
As Jonathan said, there's no such built-in feature, and exception are preferred over return codes. However, you can implement such a check at run time: [...]
Thank you all for your insight. I think I have all the information I need.
Feb 21 2016