How to initialize immutable variables with an expression that throws an exception to catch?
Nick Treleaven
nick at geany.org
Thu Apr 9 16:08:33 UTC 2020
On Friday, 3 April 2020 at 06:56:27 UTC, FeepingCreature wrote:
> 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?
Sounds like making try an expression:
immutable struct S { }
...
S s =
try
fun(); // implicit result
catch (Exception)
return;
call(s);
The above could also work if the catch block had the result and
the try block was a terminating statement.
Another option would be a new expression that either yields the
first expression if no exception was thrown, or it catches the
exception and executes a statement or block that must terminate:
S s = fun() __or_catch (Exception e) return;
call(s);
(I have seen a similar form of expression for unwrapping optional
values, terminating if the value doesn't exist in V language).
Not sure whether these are worth having yet as I've only just
considered them ;-)
More information about the Digitalmars-d
mailing list