JeffreySarnoff/NamedTupleTools.jl

Implement `fieldtypes` and `valtype` also for Tuples

simonschoelly opened this issue · 4 comments

I know, this package is for NamedTuples, but would it make sense to implement fieldtypes and valtype also for Tuples? Apparently fieldtypes is already works in Base for Type{<:Tuple} but not for Tuple.

This would make it easier to use Tuple and NamedTuple interchangable.

do you mean this?

Base.fieldtypes(x::Tuple) = typeof.(x)
Base.valtype(x::Tuple) = Tuple{typeof.(x)...}
tup = (1, "two")
fieldtypes(tup) == (Int64, String)
valtype(tup) == Tuple{Int64,String}

Yes, would it make sense to add these methods do this package?

Btw. I think the second definition is easier:

Base.valtype(x::Tuple) = x

I am trying to get away from type piracy.

Re:valtype
the second definition is different

julia> tup = (1, "two")
(1, "two")
julia> Tuple{typeof.(tup)...}
Tuple{Int64, String}

how do you feel about

valtypes(x::Tuple) = Union{typeof.(x)...}
valtypes(x::AbstractDict) = Union{typeof.(values(x))...}
valtypes(x::NamedTuple) = Union{fieldtypes(x)...}
valtypes(x::AbstractArray{T,N}) where {T,N} = T===Any ? Union{typeof.(x)...} : T