Julia: scope of keyword argument default values

Created on 2 Jul 2016  路  7Comments  路  Source: JuliaLang/julia

a=1
f(;a=a)=a
f() will generate "UndefVarError" error.
There is no problem if it is a normal argument.

breaking design keyword arguments

Most helpful comment

Resolved: change to "let*" (successive nested scopes) scoping.

All 7 comments

Could you provide the output of versioninfo(), as i cannot reproduce the error. I can reproduce this on master. The reason for the error is it gets lowered to something like the following:

a=1
function test()
      a=a
end
test()

This will also fail with an UndefVarError. I think this have been discussed somewhere, and is intended behavior. If it should work for the keyword argument then it would require some special lowering around https://github.com/JuliaLang/julia/blob/master/src/julia-syntax.scm#L427.

I think whether it is intended or not, f(a=a) and f(;a=a) should be consistent: both are OK or both are error. Current situation is confusing.

+1 for consistent behaviour. (obviously)

Tentativly marking this as a regression due for 0.5.

+1 for consistency too, but it fails on Julia 0.4.5, so it's not a regression. Furthermore, the docs say it's intended behaviour

Optional and keyword arguments differ slightly in how their default values are evaluated. When optional argument default expressions are evaluated, only previous arguments are in scope. In contrast, all the arguments are in scope when keyword arguments default expressions are evaluated.

I don't understand why all keyword arguments are in scope if their value is undefined...

I'm all for changing this in the future, but it will have to be in 0.6.

Resolved: change to "let*" (successive nested scopes) scoping.

Was this page helpful?
0 / 5 - 0 ratings