Call-ie return on behalf of caller?
Artur Skawina via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 4 04:41:46 PDT 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
More information about the Digitalmars-d-learn
mailing list