The function get
in dict.jl
has the following signature:
function get(default::Callable, h::Dict{K,V}, key) where V where K
I am wondering: what does Callable
mean? Perhaps Union{Function,Type}
? Also, does it specialize? It seems like get
is a function that ought to specialize for better performance. In other words, why is this not written as:
function get(default::T, h::Dict{K,V}, key) where T where V where K
(I am rewriting the sorted container code in DataStructures.jl and would like to follow an appropriate pattern for get
and get!
)
2 Likes
See discussion in #43491. The short answer is that it’s Union{Function,Type}
, like you’d guessed, but that you really should avoid using it unless ambiguities would arise otherwise. If we were better people, we probably would have already removed ::Callable
from existence. But we haven’t gotten around to it yet.
Since the body of the function would presumably include default(key)
, I think the function will choose to specialize on typeof(default)
whether or not you use the ::T
annotation. But I could be mistaken. You can try it with and without to see if it makes a difference. Be sure to restart julia before finalizing your decision, because sometimes the performance doesn’t update properly in this case from Revise
alone.
4 Likes