Const initialization issue, looking for solution
Jonathan M Davis
jmdavisProg at gmx.com
Wed May 29 12:08:24 PDT 2013
On Wednesday, May 29, 2013 14:36:18 Jakob Ovrum wrote:
> Consider the following example:
>
> http://dpaste.dzfl.pl/a0595ddf
> ----
> // Can throw, and we want to catch
> int createTheVar();
>
> // Can also throw, but we don't want to catch it here
> int transform(int a);
>
> int foo()
> {
> const(int) i;
>
> try
> {
> i = createTheVar(); // Clearly not allowed
> }
> catch(Exception e) // For demo purposes
> {
> // Exception handling code
> }
>
> return transform(i);
> }
> ----
Wrap the try-catch in a function.
int foo()
{
int initI()
{
try
return createTheVar();
catch(Exception e)
return int.init;
}
const int i = initI();
return transform(i);
}
And as it's a nested function, you can even have access to foo's scope (or
make it static if you don't need that).
- Jonathan M Davis
More information about the Digitalmars-d
mailing list