Call-ie return on behalf of caller?
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 3 20:20:28 PDT 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");
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150604/496465ce/attachment-0001.sig>
More information about the Digitalmars-d-learn
mailing list