Class Type Parameters
Christopher Wright
dhasenan at gmail.com
Mon Jan 21 07:03:44 PST 2008
Hans-Eric Grönlund wrote:
>> retryable(SomeExceptionClass)!(args...);
>
> I of course meant
>
> retryable!(SomeExceptionClass)(args...);
>
> Hans-Eric Grönlund Wrote:
>
>> Ah, you're right. I should have been more specific.
>>
>> What I wanted to do was to imitate a ruby-method that accepts a code block, runs it and retries n times if an exception is thrown. Like the ruby-method, I wanted to let the user decide what type of exception to catch by giving it as an argument. I explain it in more detail on my weblog:
>>
>> http://www.hans-eric.com/2008/01/17/loop-abstractions-in-d/
>>
>> For that reason, I guess the compile time solution (templates) is to prefer in this case, although as a user of the function, I would have preferred a non-template syntax.
>>
>> Like this
>>
>> retryable(SomeExceptionClass, args...);
>>
>> over this
>>
>> retryable(SomeExceptionClass)!(args...);
>>
>>
>> Cheers!
Closest you could come, if you really didn't want to use a template, is:
void retryable(ClassInfo info, int times, void delegate() totry) {
for (int i = 0; i < times; i++) {
try {
totry();
break;
} catch (Exception e) {
if (e.classinfo is info) continue; else throw e;
}
}
}
retryable(AbandonedMutexException.classinfo, 5, {writefln("Hi!");});
Typeid wouldn't work; it goes by the declared type, IIRC.
More information about the Digitalmars-d-learn
mailing list