Slow to learning newer julia syntax and scoping.
In Julia v1.1.1
what is the explanation for why the MWE below throws error "ff not defined" ?
N = 5;
typp = "b";
mu = 2;
function bigfun()
function f(u,mu)
ee = mu*u;
return ee
end
function g(uv,mu)
ee = (mu^2)*uv
return ee;
end
while 1 == 1
u = ones(N);
if typp == "a"
ff(u) = f(u,mu);
elseif typp == "b"
ff(u) = g(u,mu);
end
fu = ff(u);
break;
end
end
bigfun();
This is a known bug in Julia: https://github.com/JuliaLang/julia/issues/15602. You can't define an inner function conditionally. There a few ways around this issue:
ff
as an anonymous function:
if typp == "a"
ff = u -> f(u,mu)
elseif typp == "b"
ff = u -> g(u,mu)
end
fu = ff(u)
ff
once, and add the conditional inside of it:
function ff(u, typp)
if typp == "a"
f(u,mu)
elseif typp == "b"
g(u,mu)
end
end
fu = ff(u, typp)
ff
at all. You don't need to, in the example you provided, just assign fu
conditionally
if typp == "a"
fu = f(u,mu)
elseif typp == "b"
fu = g(u,mu)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With