Thread overview
Call-ie return on behalf of caller?
Jun 03, 2015
Tofu Ninja
Jun 04, 2015
ketmar
Jun 04, 2015
Tofu Ninja
Jun 04, 2015
ketmar
Jun 04, 2015
Artur Skawina
June 03, 2015
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?
June 04, 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. ;-)

June 04, 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:
>
>> 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...
June 04, 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:
>> 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...

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 = tmp();
  }

  void test (string s) {
    import std.stdio;
    writeln(s);
  }

  void main () {
    mixin callit!(() => test("wow"));
    mixin callit!(() => 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");
  }


June 04, 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