digitalmars.D.learn - Call-ie return on behalf of caller?
- Tofu Ninja (15/15) Jun 03 2015 Is there a way other than exceptions for a called function to
- ketmar (2/4) Jun 03 2015 write mixin template for that. ;-)=
- Tofu Ninja (2/6) Jun 03 2015 Except mixin templates can only be used for declarations...
- ketmar (32/41) Jun 03 2015 you can cheat a compiler (warning! don't try that at home!):
- Artur Skawina via Digitalmars-d-learn (10/19) Jun 04 2015 Without a preprocessing step, you probably won't find anything
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
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
On Thursday, 4 June 2015 at 01:01:08 UTC, ketmar wrote:On Wed, 03 Jun 2015 22:37:52 +0000, Tofu Ninja wrote:Except mixin templates can only be used for declarations...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
On Thu, 04 Jun 2015 01:07:10 +0000, Tofu Ninja wrote:On Thursday, 4 June 2015 at 01:01:08 UTC, ketmar wrote: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"); } =On Wed, 03 Jun 2015 22:37:52 +0000, Tofu Ninja wrote:=20 Except mixin templates can only be used for declarations...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
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