No new type needed. You can also get type-stable code with:
linear_combinations(f, c) = (x...; kw...) -> map(c, f) do c, f
c*f(x...; kw...)
end |> sum
Defining new callable types (“functors”) can be useful for other reasons, of course; see How are functors useful? - #6 by stevengj
The trick here is that you construct tuples from tuples (map
has specialized methods for tuples), rather than generators foo for (c, f) in zip(...)
. I believe zip
by itself is also type-unstable for heterogeneous tuples, for that matter.