Strict typing and restricting types in a method's signature

Could you use generated functions?
Not sure if this is recommended, or indeed if it will work.

@generated function myfunc{T}(a::T)
    if method_exists(feature, (T,))
        :(_myfunc(a))
    else
        :(error("$T does not have feature"))
    end
end

function _myfunc(a)
    println("_myfunc called with $(typeof(a))")
end

feature(x::Number) = x*x


myfunc(2)
myfunc("cat")

4 Likes