Class Type Parameters
Jarrett Billingsley
kb3ctd2 at yahoo.com
Mon Jan 21 07:57:34 PST 2008
"Christopher Wright" <dhasenan at gmail.com> wrote in message
news:fn2bqa$25rl$1 at digitalmars.com...
>
> 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!");});
The only possible issue I could see with that would be if an exception were
thrown that was derived from the type that you passed in, which, according
to normal exception behavior, should still be caught. What you have to do
then is perform a dynamic cast. There's no syntax for this, but you can
hack around with some of the runtime functions to do the same thing. Place:
extern(C) Object _d_dynamic_cast(Object o, ClassInfo c);
somewhere in your module, then in the catch clause of retryable:
catch(Exception e)
{
if(_d_dynamic_cast(e, info))
continue;
else
throw e;
}
That _should_ work. It's doing the same thing as the cast operator would
but without the pretty syntax.
Also, this function would be a great place to use a lazy void parameter for
the toTry parameter ;)
More information about the Digitalmars-d-learn
mailing list