This is a follow up question which cristallized from this other discourse topic
Problem
I am struggeling with improving performance of my package ExtensibleEffects.jl because Julia is not specializing a function on types in certain cases. See the docs.
For me it feels, this case should not avoid specialization, but anyway, in current Julia this is what happens. Hence I am looking for a way to tell the compiler it should specialize on types, however same time I want to keep my generic function.
I.e. the recommendation from the docs, to just use myfunc(::Type{T}) where T = ...
in order to trigger specialization does not work for me, because I need my function myfunc
to stay generic.
What other way exist to mark a function for type-specialization?
Example
Here my concrete minimized example which fails to infer for the type input, but runs smoothly for a normal input. It is adapted from ExtensibleEffects. ChainedFunctions
essentially represents function composition without composing functions, but just by storing functions. As the error already happens without composition, the example feels incomplete, but please take it for granted, that this is useful for ExtensibleEffects.jl.
struct ChainedFunctions{Fs}
functions::Fs
ChainedFunctions(functions...) = new{typeof(functions)}(functions)
end
myconvert(T, value) = T(value)
function myconvert_wrapper(type, value)
continuation = ChainedFunctions(x -> myconvert(type, x))
first_func = Base.first(continuation.functions)
first_func(value)
end
and here the test which partly works, and partly fails.
using Test
tostring(a) = "$a"
@inferred myconvert_wrapper(tostring, 1)
# "1"
@inferred myconvert_wrapper(Symbol, 1)
# ERROR: return type Symbol does not match inferred return type Any