Local function redefinition: feature or bug?


#1

For the followed function, t() will output 12 in Julia0.4 and output 22 since Julia0.5. Is that a feature or bug? Anyway, it looks strange to output 22 and cause a hard bug after I upgrade my some old Julia code from 0.4 to 0.6.

function t()
    f()=1
    print(f())
    f()=2
    print(f())
end

#2

Please quote your code with backticks

This is indeed confusing and is a real change since earlier Julia versions: https://github.com/JuliaLang/julia/issues/15602#issuecomment-200410836

Fortunately, it’s also very easy to fix by using an anonymous function instead (which will perform just as well):

julia> function t()
         f = () -> 1
         print(f())
         f = () -> 2
         print(f())
       end
t (generic function with 1 method)

julia> t()
12

#3

BTW, is there any documentation in the v0.7 manual about how function tables work in local scope?


#4

It could less easy with multiple dispatch:

julia> function t()
           f(x::Number)=1
           print(f(1.))
           f(x::Float64)=2
           print(f(1))
       end;t()
21