Suppose I have a function with 2 arguments, and I want to make a new function with one of the arguments fixed to a value, depending on some condition. For example:
fun(x,a) = a*x function foo(flag::Bool) if flag bar(x) = fun(x,2) else bar(x) = fun(x,2) end return bar end
In this example the value of
a is fixed to
2 on both branches, just for illustration.
If I run this code in a script,
foo(true) returns the desired function, but
ERROR: UndefVarError: bar not defined Stacktrace:  foo(::Bool) at .\none:7  eval(::Module, ::Any) at .\boot.jl:235
I realize now at least a couple of different ways to re-write
foo so that it works, but my question is why does the above version not work when control flows down the
else branch? I think I can learn something by understanding this, but I can’t make sense of it in terms of my understanding of scoping rules.
Thank you (I’m using Julia 0.6.2, BTW)