I am curious whether initializing a variable of type T using the following code is any good for type stability. The type is usually alot more complex than the example used below with significant allocations and a dependence on the conditions, so I don’t want to initialize a full instance before the if-statement with dummy input, too much waste. On the other hand, if declared inside the if statement, it will not be visible outside, and that’s a no go. I also couldn’t find a way to initialize a global-but-not-too-global variable inside an if statement, so the following seems like the next most reasonable approach.
if declared inside the if statement, it will not be visible outside
I don’t think this is true - if blocks don’t introduce new scope. Do you have a minimum working example of a case where you can’t access something allocated inside an if block?
I had a few cases before where declaring a variable with nothing seemed to have solved an error, so I thought this was true. Thanks for correcting me. I will remove this declaration.