I’d like to apply a function to all type parameters of any given tuple type. I don’t know the size of the tuples, I would just like to accumulate the function over all type parameters. However, I cannot find a way to do this in an type inference-friendly way.
Here’s an example, I’m probably missing something obvious: right now I’m using the parameters
attribute of a type.
myproperty(::Type) = false
myproperty(::Type{<:String}) = true
myproperty(::Type{<:Integer}) = true
myproperty(::Type{T}) where T <: Tuple = all(myproperty.(T.parameters))
myproperty(::T) where T = myproperty(T) # for convenience
myproperty((1, 2, 3)) # true
myproperty((1, 2, "ciao")) # true
myproperty((1, 2.7, "ciao")) # false
However, the following fails:
using Test
@inferred myproperty((1, 2.7, "ciao"))
# ERROR: return type Bool does not match inferred return type Union{Missing, Bool}
If I check what the parameters
attribute is, I understand that this is a SimpleVector
:
julia> typeof((1, 2.7, "ciao")).parameters
svec(Int64, Float64, String)
Any way one could get a Tuple
of the type parameters, so that what I’m trying to do above is type-stable?
Edit: even using fieldtypes
as follows doesn’t seem to fix this
myproperty(::Type{T}) where T <: Tuple = all(myproperty.(fieldtypes(T)))