I would like to define the function f(x, t::Type) , which performs a different behavior depending on whether, isa(x, t) . Let's say that I want to call b1(x) , if so, and b2(x) otherwise.
I know that I can perform a dynamic check at runtime as follows:
function f(x, t::Type) if isa(x, t) b1(x) else b2(x) end end
However, is there a way to do this exclusively with the parametric send method? For example, if I define
f{T}(x::T, t::Type{T}) = b1(x) f(x, t::Type) = b2(x)
for f(1, Int) and f(1.0, Int) correct behavior is invoked. But I want this to work for all t subtypes as well:
f(1, Number)
This actually calls b2 because the first signature of f does not match. Interestingly, however, f(x::Number, t::Type{Number}) = b1(x) will match in this case.
Did I miss something obvious here?
This was a bug, and it was fixed in 0.4.
Questions:
Why does f{T}(x::T, t::Type{T}) not match for f(1, Number) , although there is a type replacement for t ( Number ) that will match?
Using f{T2, T1 <: T2}(x::T1, t::Type{T2}) or something similar does not work, because all static parameters enter the scope only after closing the full list of static parameters. Why?
Are there any penalties for the effectiveness of using a dynamic approach?
How to define methods as an internal function, so I can bind t to a local variable, for example: function f(x, t::Type); g(x::t) = b1(x); g(x) = b2(x); g(x) end function f(x, t::Type); g(x::t) = b1(x); g(x) = b2(x); g(x) end
It works, but what are the costs of performance?
What is the idiomatic / preferred way to solve this problem?
(I tried this on 0.3.2.)
source share