# Julia doesn't catch undefined variables at compile time?

**URL:** https://discourse.julialang.org/t/julia-doesnt-catch-undefined-variables-at-compile-time/2616
**Category:** General Usage
**Created:** [March 12, 2017, 5:55am UTC](https://discourse.julialang.org/t/julia-doesnt-catch-undefined-variables-at-compile-time/2616 "2017-03-12T05:55:34Z")
**Posts on this page:** 4
**Page:** 2

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [March 13, 2017, 8:13pm UTC](https://discourse.julialang.org/t/julia-doesnt-catch-undefined-variables-at-compile-time/2616/21 "2017-03-13T20:13:36Z")

</div>

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

```julia

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

```

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [March 16, 2017, 6:31am UTC](https://discourse.julialang.org/t/julia-doesnt-catch-undefined-variables-at-compile-time/2616/22 "2017-03-16T06:31:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [March 16, 2017, 12:45pm UTC](https://discourse.julialang.org/t/julia-doesnt-catch-undefined-variables-at-compile-time/2616/23 "2017-03-16T12:45:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)
#### Post date: [March 16, 2017, 12:53pm UTC](https://discourse.julialang.org/t/julia-doesnt-catch-undefined-variables-at-compile-time/2616/24 "2017-03-16T12:53:31Z")

</div>

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.

[Previous page](https://discourse.julialang.org/t/julia-doesnt-catch-undefined-variables-at-compile-time/2616.md?page=1)
