Weird behaviour with function variables leaking out of if blocks

According to the docs, if blocks are leaky. I can use it to my advantage with the following function f, but not with g, why?

@enum Case A B

f = function (case, x)
    if case == A
        coeff = 3
    elseif case == B
        coeff = 8
    end
    coeff * x
end

g = function (case, x)
    if case == A
        lambda(x) = 2x
    elseif case == B
        lambda(x) = x^2
    end
    lambda(x)
end

f(A, 5) #  15
f(B, 5) #  40

g(A, 5) #  25 .. wait, what?
g(B, 5) #  ERROR: lambda not defined. 
2 Likes

Note that g(A, 5) passed with no warning and a completely undesired behaviour :\ Is there anything I can turn on to avoid this being silent?

we should definitely make this a parser error.

5 Likes