-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NamedTuples changed iteration behavior for vararg keywords #25177
Comments
It looks like the correct way to do this in a cross-version way is: using Compat: pairs
function foo(;kw...)
for (k, v) in pairs(kw)
@show (k, v)
end
end |
Perhaps we should call the inner function with the object |
That would definitely make adapting existing code easier. |
I think this was known, but it's annoying as a breaking change. Wrapping in |
no, it'll still maintain it's new abilities from now being a NamedTuple |
That does not seem to be true at all, @vtjnash: julia> f(; kws...) = pairs(kws)
f (generic function with 1 method)
julia> kws = f(a = 1, b = 2)
Base.Generator{Base.Iterators.Zip2{Tuple{Symbol,Symbol},Tuple{Int64,Int64}},getfield(Base, Symbol("##7#8")){Pair}}(getfield(Base, Symbol("##7#8")){Pair}(), Base.Iterators.Zip2{Tuple{Symbol,Symbol},Tuple{Int64,Int64}}((:a, :b), (1, 2)))
julia> kws.a
ERROR: type Generator has no field a |
You don’t seem to be on master |
I'm on a day-old version of master so unless this is more recent than that. |
Fresh master has the same error, although it does print the key-value map in the pairs object: julia> f(; kws...) = pairs(kws)
f (generic function with 1 method)
julia> kws = f(a = 1, b = 2)
Base.Iterators.IndexValue{Symbol,Int64,Tuple{Symbol,Symbol},NamedTuple{(:a, :b),Tuple{Int64,Int64}}} with 2 entries:
:a => 1
:b => 2
julia> kws.a
ERROR: type IndexValue has no field a
Stacktrace:
[1] getproperty(::Base.Iterators.IndexValue{Symbol,Int64,Tuple{Symbol,Symbol},NamedTuple{(:a, :b),Tuple{Int64,Int64}}}, ::Symbol) at ./sysimg.jl:8
[2] top-level scope |
|
This was fixed by #25290 |
This seems to be a consequence of #24795
In previous Julia versions, we could iterate through vararg keywords as key-value tuples:
but after #24795, iterating through vararg keywords only gives the values:
If this is an intended change, then it should probably at least be mentioned in NEWS.md. I realize that the changelog mentions that "name-value pairs can be iterated using pairs(kw)" but I think it's important to clarify that this is a breaking change.
The text was updated successfully, but these errors were encountered: