pancho
pancho

Reputation: 211

Julia - UnderVarError

The following code throws an "UndefVarError: g not defined"

function asdf()
if true
    f(t) = t
else 
    g(t) = t
    f(t) = g(t)
end
return f
end
w = asdf()
w(1)

but by replacing f(t) = g(t) by f = g, it works. Why?

Upvotes: 2

Views: 341

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69819

This is a known bug https://github.com/JuliaLang/julia/issues/15602.

The short recommendation is not to define a function that goes to method table twice in the body of a function. Instead use a variable to which you assign two different functions (with different names or anonymous) in branches.

What you should do until this is fixed is:

function asdf()
    if true
        f = t -> t
    else false
        g(t) = t
        f = g(t)
    end
    return f
end

Upvotes: 2

Related Questions