|
|
|
|
|
by m_mueller
3542 days ago
|
|
I agree with with{}, less so with try/finally. First of all, I think exceptions are at least as easy to get wrong in your application (or worse system) design as goto. And if you get them wrong, it's probably even harder to debug. Second of all, the resulting code is IMO far less readable. To illustrate - which would you prefer: a = None
try:
a = get_ressource_a()
b = None
try:
b = get_ressource_b(a)
process(a,b)
except BException as e:
handle_errors(e)
finally:
cleanup_b(b)
except AException as e:
handle_errors(e)
finally:
cleanup_a(a)
or a = get_ressource_a()
if not a:
handle_errors()
return
b = get_ressource_b(a)
if not b:
handle_errors()
goto cleanup_a
process(a,b)
cleanup_b:
#do the cleanup
cleanup_a:
#do the cleanup
My dream language would have more or less Python's syntax, with dynamic, inferred, but strong types, no exception handling, and instead Swift's "optional" baked into the language and a very smart logging that I can peak into with the error handler. The error handler could be the only sideeffect allowed when a function is declared pure. I don't need goto if with{} works well with such a system. |
|
It provides nearly everything you mention and covers the "finally" case with `defer`. Worth looking at - I re-implemented suckless' slock in Nim[2][3] and it was very pleasant experience.