www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Call-ie return on behalf of caller?

reply "Tofu Ninja" <emmons0 purdue.edu> writes:
Is there a way other than exceptions for a called function to 
force the caller to return?

Specifically, I know the return type of the caller(its always 
bool) and under certain circumstances I would like the caller to 
just give up and return false if the called function fails. With 
as little boiler plate as possible on the call site. In c++ I 
could do it very easily with macros, but I am failing to see a 
similar way to do it in D other than maybe string mixins but that 
seems like a bit much to do every time I want to call this thing.

I would be willing to put some boilerplate code in the beginning 
of the caller, I could just put that in a mixin.

I know exceptions are really what I should be using but they are 
such a pain to work with. I don't want to have to put try catch 
blocks every time I call the caller.

Any ideas?
Jun 03 2015
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Wed, 03 Jun 2015 22:37:52 +0000, Tofu Ninja wrote:

 I don't want to have to put try catch blocks every
 time I call the caller.
write mixin template for that. ;-)=
Jun 03 2015
parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
On Thursday, 4 June 2015 at 01:01:08 UTC, ketmar wrote:
 On Wed, 03 Jun 2015 22:37:52 +0000, Tofu Ninja wrote:

 I don't want to have to put try catch blocks every
 time I call the caller.
write mixin template for that. ;-)
Except mixin templates can only be used for declarations...
Jun 03 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Thu, 04 Jun 2015 01:07:10 +0000, Tofu Ninja wrote:

 On Thursday, 4 June 2015 at 01:01:08 UTC, ketmar wrote:
 On Wed, 03 Jun 2015 22:37:52 +0000, Tofu Ninja wrote:

 I don't want to have to put try catch blocks every time I call the
 caller.
write mixin template for that. ;-)
=20 Except mixin templates can only be used for declarations...
you can cheat a compiler (warning! don't try that at home!): mixin template callit(alias fn) { private auto tmp () { try fn(); catch (Exception) {} return 0; } private auto tmpv =3D tmp(); } void test (string s) { import std.stdio; writeln(s); } void main () { mixin callit!(() =3D> test("wow")); mixin callit!(() =3D> test("hey")); } but actually, simple template will do the work too: template callit(alias fn, A...) { void callit (A args) { try fn(args); catch (Exception) {} } } void test (string s) { import std.stdio; writeln(s); } void main () { callit!test("wow"); callit!test("hey"); } =
Jun 03 2015
prev sibling parent Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On 06/04/15 00:37, Tofu Ninja via Digitalmars-d-learn wrote:
 Is there a way other than exceptions for a called function to force the caller
to return?
 
 Specifically, I know the return type of the caller(its always bool) and under
certain circumstances I would like the caller to just give up and return false
if the called function fails. With as little boiler plate as possible on the
call site. In c++ I could do it very easily with macros, but I am failing to
see a similar way to do it in D other than maybe string mixins but that seems
like a bit much to do every time I want to call this thing.
 
 I would be willing to put some boilerplate code in the beginning of the
caller, I could just put that in a mixin.
 
 I know exceptions are really what I should be using but they are such a pain
to work with. I don't want to have to put try catch blocks every time I call
the caller.
 
 Any ideas?
Without a preprocessing step, you probably won't find anything that's much better than: enum caught(string C, string R = "typeof(return).init") = `try {`~C~`} catch return (`~R~`);`; bool f(int a) nothrow { mixin (caught!q{ code_that_throws(a); }); return 1; } artur
Jun 04 2015