Julia doesn't catch undefined variables at compile time?

A macro based solution could do the trick, so defensive coders can opt-in, here’s a quick one based on Lint.jl:


using Lint

function _noglobal(ex::Expr)
    ctx = LintContext()
    Lint.lintexpr(ex,ctx)

    for m in ctx.messages
        m.code == :E321 && error(string(m.message,": ",m.variable))
    end
    ex
end

macro noglobal(ex)
    _noglobal(ex)
end

@noglobal function foo()
    x = 1
    if x < 1
        println("less than 1")
        println(bar)
    else
        println("greater than or equal to 1")
    end
end

ERROR: use of undeclared symbol: bar
1 Like

This is… certainly an interesting use of Lint. If there is demand for such an API, we could standardize one and document it. Currently, such code is fairly susceptible to break due to internal changes in Lint.

I actually find it quite useful, so I think it would be a good idea yes.

the problem with this is that it should be used for almost all functions to be of any benefit. the code will definitely look very ugly and noisy.