Search code examples
dictionarytypesjuliakeyword-argument

Difference between Dict and Base.Iterators.Pairs in Julia


In Julia v1.0 when using keyword arguments the resulting structure in the function will have the type Base.Iterators.Pairs.

julia> foo(;kwargs...) = println(kwargs)
julia> foo(a = 1, b = 2)
Base.Iterators.Pairs(:a=>1,:b=>2)

What is the difference between Iterators.Pairs and usual Dictionary? Why do we use this specific type?


Solution

  • In Julia 0.7/1.0, keyword arguments were changed to be stored as named tuples instead of dictionaries. The Pairs type is just a wrapper so that iterating gives you key,value pairs (iterating over a named tuple just gives values, like iterating over a tuple).