function foo(x::Int; d=:out)
myfn = (d == :out) ? fn1 : fn2
z = 0
for i = 1:x
z += myfn(i)
end
end
I understand the inefficiency that results from the compiler not being able to tell that myfn won’t change within the loop. Is there a way to do this efficiently that preserves the simplicity of the function? I tried wrapping the loop inside a let myfn = (d == :out) ? fn1 : fn2 block but that didn’t show any benchmarking improvements.
I would assume not because the keyword argument would not be specialized on. You could write two copies of the function: one with positional arguments, and the other with keyword arguments.
You’re right; I was a little sloppy with my language. The types of keyword arguments are not specialized on, which is what is important for this question.
Then language you use does mean that but it’s not the case (i.e. what you are claiming is wrong). If you read it somewhere in the doc then the doc must be corrected.
But only one method/function is compiled in my example above (see the “generic function with 1 method” output after the second line), even though the kwarg has a different type. This implies that the type of the kwarg is ignored when determining the set of input types, right?