Can anyone enlighten me as to why this bit of code spits back that the X is unsafe in 'try', well I know why, but more so how to fix it.
try X = lists:append(lists:zipwith3(fun(X, Y, Z) -> [X, Y, Z] end, Data1, Data2, Data3)) of
MP -> X
catch K -> (X = 0)
end.
%MP = [lists:zipwith3(X, Y, Z) || X, Y, Z <- [Data1, Data2, Data3]],
P = X
The simplest way to fix it is to put the assignment outside of the try-catch:
X =
try lists:append(lists:zipwith3(fun(X, Y, Z) -> [X, Y, Z] end, Data1, Data2, Data3)) of
MP -> MP
catch K -> 0
end.
As for why this happens, the chapter on expressions in the Erlang reference manual says:
For the
try
expression variable scoping is limited so that variables bound in the expression are always 'unsafe' outside the expression.
It used to say "This is to be improved", but that was removed in this commit.