How to initialize immutable variables with an expression that throws an exception to catch?

FeepingCreature feepingcreature at gmail.com
Fri Apr 3 06:56:27 UTC 2020


Consider the following code:

struct S { }
...
   S s = void;
   try
     s = fun();
   catch (Exception)
     return;
   call(s);

Now you change S to be immutable.

How are you supposed to initialize s? You can't assign to it 
anymore.

Assume that we explicitly don't want to pull the call into the 
try body, for instance because we want exceptions from call to 
not be caught.

The only way I've found is to make fun() return Algebraic!(S, 
Exception) but that's kind of ugly and bypasses a basic language 
feature.

Maybe D could allow to initialize an immutable variable from the 
try{} body if the catch{} body is statically known to exit by 
return/throw?


More information about the Digitalmars-d mailing list