Erlang – checking for unbound variables-Collection of common programming errors
If you (try to) refer to an unbound variable in an expression, it’s a compilation error. In particular, after
{ok, After} = ts_dynvars:lookup(last, DynVars),
there are only two possibilities: if the pattern matches, After
is bound, and can be used; if it doesn’t, an exception will be thrown, and code which tries to work with After
will never be executed.
UPDATE:
are you telling me there is no way to branch code execution in the situation in which the pattern does not match
Of course there is:
case ts_dynvars:lookup(last, DynVars) of
{ok, After} -> ...;
_ -> ... %% or other patterns
end
but the compiler won’t let you use After
in other branches or after case
(unless all branches bind After
).
is this exception not catchable at all
It is:
try
{ok, After} = ts_dynvars:lookup(last, DynVars),
...
catch
_:_ -> ...
end
but again, you won’t be able to use After
in catch
sections or after try
ends (you can bind a new variable named After
, of course).