What does having local function in say a module mean?
I think this syntax should be disallowed. It is not documented what it does. I have opened a discussion here to clarify this issue.
Is it private is it public?
This syntax - as far as I can tell - defines a function that is not introduced into a global methods table of the module. Therefore effectively this definition is similar to defining an anonymous function, so you can do something like this:
julia> x = (local f() = 1)
(::getfield(Main, Symbol("#f#3"))) (generic function with 1 method)
julia> x()
1
Are not all functions declared local by default for the scope that they are declared in?
Yes. You are not allowed to add a method to a global method table from a local scope. The only way to do it is by using eval
which is in general not recommended in normal code.
And if that is the case the local keyword is redundant for functions?
No, it is not fully redundant, as sometimes it is needed in nested local scopes (as in general local
is needed in local scopes for any variable name). See this example:
julia> function f1()
f2() = 10
for i in 1:2
f2() = i
println(f2())
end
end
f1 (generic function with 1 method)
julia> f1()
ERROR: UndefVarError: i not defined
Stacktrace:
[1] f1() at .\REPL[3]:2
[2] top-level scope at none:0
julia> function f1()
f2() = 10
for i in 1:2
local f2() = i
println(f2())
end
end
f1 (generic function with 1 method)
julia> f1()
1
2
EDIT
As noted on Discourse here is an explanation of this issue https://github.com/JuliaLang/julia/issues/10472#issuecomment-321584501 on GitHub.
In summary - it works as I have assumed, and the technical reason is that if Julia sees local
keyword in global scope an implicit local scope is created so values can leak out of this scope but not variable bindings.