I have a Tuple
and want to replace its starting segment:
function switch_front(new_front, t)
n = length(t)
k = length(new_front)
@assert k <= n
(new_front..., t[k+1:n]...)
end
using Test
@test switch_front((10,), (1, 2, 3, 4)) === (10, 2, 3, 4)
@test switch_front((10, 20), (1, 2, 3, 4)) === (10, 20, 3, 4)
@test switch_front((10, 20, 30), (1, 2, 3, 4)) === (10, 20, 30, 4)
For very small tuples this works fine. However already for length 4 tuple inference breaks down:
function funny_numbers(n)
types = [
Int128, Int16, Int32, Int64, Int8,
UInt128, UInt16, UInt32, UInt64, UInt8,
Float16, Float32, Float64,
]
tuple([T(true) for T in rand(types, n)]...)
end
@inferred switch_front(funny_numbers(3), funny_numbers(3)) # works
@inferred switch_front(funny_numbers(2), funny_numbers(4)) # works
@inferred switch_front(funny_numbers(3), funny_numbers(4)) # fails
Is there a way to make this infer much better? I would like this to work for length 30 tuples. Can it be done without using @generated
?