Argument type for tuples of same length

I would like to create a function taking two tuples t1 and t2 as arguments. The two tuples need to have the same length, but their elements can be of any type, and each tuple doesn’t need to be composed of the entries of the same type (i.e., the tuples can be inhomogeneous). An example satisfying this condition would be t1 = (1, 1.0, true) and t2 = (zeros(3), "abc", 1//2).

Is it possible to specify the argument types enforcing this condition? I thought

function myfun(t1::NTuple{N}, t2::NTuple{N}) where {N}

would do the job, but it seems that ::NTuple expects a homogeneous tuple (i.e., a tuple composed of entries of the same type).

function myfun(t1::Tuple{Vararg{Any,N}}, t2::Tuple{Vararg{Any,N}}) where {N}
2 Likes

Thanks. I notice that

function myfun(t1::NTuple{N,Any}, t2::NTuple{N,Any}) where {N}

also works. But then this seems a bit strange for me. I thought the function signature in my original posting is equivalent to

function myfun(t1::NTuple{N,T}, t2::NTuple{N,S}) where {N,T,S}

but then it seems that Julia cannot infer that T = Any and S = Any. Am I missing something?

1 Like