Is there a way to find the most concrete type representation of a variable and apply it ?
For example
say we got our hands to a Dict{Any, Any}
julia> d = Dict{Any,Any}("one" => 1, "two" => 2)
Dict{Any, Any} with 2 entries:
"two" => 2
"one" => 1
it would be great if we could do
julia> conretisize(d)
Dict{String, Int64} with 2 entries:
"two" => 2
"one" => 1
and get back the most accurate typed version.
Similarly, Number[1, 2.0, 3]
could be transformed to Float64[]
and maybe Vector{Any}([1,missing, 2])
could be transformed to something like Vector{Union{Missing, Int64}}
.
Remarks
Ofc, one could customize such an operation w.r.t. the situation. E.g., above we could get the type stable representation by doing Dict(k => v for (k,v) in d)
for Dict
s and [v for v in vector]
for Vector
s.
But I am wondering whether a more general implementation can be provided, that would work for all types.
If generalizing is hard, do you think it’s worth to provide it as an interface that could/should be implemented by the developers for newly defined structs ?
It’s obvious that conretisize
cannot always return concrete type, so maybe the naming is a bit misleading.
As I understand it, this operation cannot be done in place as the memory representation changes.
I think this could be very useful to call in the middle of some functions to kill some type instabilities, if used together with the function barrier pattern.
(some more context in zulip mentioned as trytoinfer
, but this post can be treated independently.)