Type Inference and Try Blocks

user1234 user12324 at 1234.de
Tue Jan 21 02:27:51 UTC 2020


On Monday, 20 January 2020 at 23:16:07 UTC, Henry Claesson wrote:
> This isn't a D-specific "problem", but there may be D-specific 
> solutions.
> I have a function `doSomething()` that returns a Voldemort 
> type, and this same function also throws. So, there's this:
>
> try {
>     auto foo = doSomething();
> } catch (AnException e) {
>     // Do stuff
> }
>
> The problem that I'm encountering is that I'd like, assuming no 
> exception was thrown, to use foo outside the `try` (or 
> `finally`) block to avoid nesting as any operations on `foo` 
> from that point onward may also throw. Are there any constructs 
> that act as alternatives to try/catch/finally so that I can do 
> this?
>
> (This issue could very well stem from poor design and not being 
> familiar with programming using exceptions. So feel free to 
> ignore.)
>
> Thanks

The problem you have is not called type inference, it's called 
"scope".
In a particular scope you have symbols "S". In a sub scope you 
also have "S" but in the parent scope "S" are not existing 
anymore.



More information about the Digitalmars-d-learn mailing list