request switch statement with common block

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Aug 4 06:46:02 PDT 2013


On Sat, Aug 03, 2013 at 03:18:49PM -0700, Jonathan M Davis wrote:
[...]
> Hmmm. For a scope(success) statement to work, the code has to be put
> at every successful exit point from the scope. In the simple case,
> that means putting it at the end of the try portion of a try-catch
> block, but the try-catch isn't strictly necessary if there is no
> scope(failure) or scope(exit) - though I don't know what dmd currently
> does.
> 
> Where scope(success) is useful is when you have multiple return
> statements and want something to occur when they return before exiting
> the function (or possibly the same thing but with break or continue
> statements inside a loop).  In that case, try-catch doesn't help at
> all, beacause in all cases, you're forced to either duplicate the code
> in the scope(success) block at each point that the code exits the
> scope or have a common block with a goto at each exit point.
> 
> Yeah. I don't see any need for try-catch there. So, it shouldn't be a
> problem. 
>
> I know that it was discussed with regards to a pull for RedBlackTree,
> but I don't think that it was a long discussion, and I guess that it
> wasn't long enough. Thinking through it now, I really don't see a
> problem with it (though maybe I'm missing something). But I'd have to
> investigate what dmd actually does to see what the actual efficiency
> impact is. It wouldn't surprise me at all if dmd currently inserts
> useless try-catch blocks or something like that (though hopefully it
> doesn't). But assuming that dmd's implementation is appropriately
> efficient, I don't see any problem with scope(success), so I may start
> using it again (though I should probably investigate what code it
> generates first).
[...]

My guess is that dmd is using the finally block to implement code that
runs before the function returns / scope exits. That's probably where
the try/catch blocks are coming from. Not terribly efficient, but
simpler to implement, I guess.



T

-- 
Chance favours the prepared mind. -- Louis Pasteur


More information about the Digitalmars-d mailing list