Const initialization issue, looking for solution
Ali Çehreli
acehreli at yahoo.com
Wed May 29 09:37:24 PDT 2013
On 05/29/2013 06:25 AM, Jakob Ovrum wrote:
> I was unable to leverage std.exception.
I have looked only your short example. std.exception.ifThrown may work:
import std.typecons;
import std.exception;
alias ToThrow = Flag!"ToThrow";
// Can throw, and we want to catch
int createTheVar(ToThrow toThrow)
{
if (toThrow == ToThrow.yes) {
throw new Exception("bad cat!");
}
return 42;
}
// Can also throw, but we don't want to catch it here
int transform(int a)
{
return a + 1;
}
int foo(ToThrow toThrow)
{
const(int) i = createTheVar(toThrow).ifThrown!Exception(666);
return transform(i);
}
unittest
{
assert(foo(ToThrow.yes) == 667);
assert(foo(ToThrow.no) == 43);
}
void main()
{}
Ali
More information about the Digitalmars-d
mailing list