andyferris / Dictionaries.jl

An alternative interface for dictionaries in Julia, for improved productivity and performance
Other
278 stars 28 forks source link

mapped dictionary has element type `Any` #99

Closed gaurav-arya closed 2 years ago

gaurav-arya commented 2 years ago

In the following code, the mapped dictionary has element type Any.

julia> using Dictionaries
julia> x = 3
3
julia> d = Dictionary([4], [5])
1-element Dictionary{Int64, Int64}
 4 │ 5
julia> map(val -> val + x, d)
1-element Dictionary{Int64, Any}
 4 │ 8

This seems to be because Core.Compiler.return_type returns Any, due to the global x:

julia> Core.Compiler.return_type(val->val+x, (Int64,))
Any

However, I would expect the element type to still be Int64 based on the fact that elements of the outputted dictionary have type Int64, which would match the behaviour of Base.map:

julia> map(val -> val + x, [4])
1-element Vector{Int64}:
 7
gaurav-arya commented 2 years ago

Just saw https://github.com/andyferris/Dictionaries.jl/issues/28; this might be related / a duplicate

andyferris commented 2 years ago

Yes, currently we are using return_type, and we might one day resolve #28.

In practice people shouldn't be using globals that way in performance-critical code, and for playing at the REPL it shouldn't have a big effect.

andyferris commented 2 years ago

Maybe I'll just close this as a duplicate of #28?