How to code-gen unrolled loop over a tuple?

Any Tuple can be regarded as subtyping some NTuple.
For example:

julia> Tuple{typeof(sin),typeof(cos),typeof(+)} <: NTuple{N,Function} where N
true

So, just use funcs::NTuple{N,Function} as the signature for the pickfunc function.

I thought NTuple{N} would do it, but it seems not:

julia> Tuple{typeof(sin),typeof(cos),typeof(+)} <: NTuple{N} where N
false

I don’t understand why…

NTuple{N} where N is an alias for NTuple{N,T} where {N,T}, so it always expects the same T. It doesn’t work for functions because each function has its own concrete type:

julia> typeof(sin)
typeof(sin) (singleton type of function sin, subtype of Function)

julia> typeof(cos)
typeof(cos) (singleton type of function cos, subtype of Function)

I guess the most generic signature you could use is NTuple{N,Any} where N, which will work for any tuple:

julia> Tuple{typeof(sin),typeof(cos),typeof(+),Float64} <: NTuple{N,Any} where N
true

This way you can also pass any callable object that doesn’t necessarily subtypes Function, but acts as one.

2 Likes