Stream: helpdesk (published)

Topic: Add more types to Tuple


view this post on Zulip Kwaku Oskin (Apr 07 2021 at 09:09):

Well, topic's name is strange, but I do not know how to name it better.

The question is the following: I have some Tuple type, for example T1 = Tuple{Int, Int, Float64}. And I want to add more types inside curly brackets, for example, Tuple{String, Int, Int, Float64}. Ideally it should be a function (or macros?) which do something like:

f(::Type{S}, ::Type{Tuple{???}}) = ??? # should produces Tuple{S, ???...}

view this post on Zulip Kwaku Oskin (Apr 07 2021 at 09:13):

May be from another angle, same question can be also asked like that. I have following structure

struct Foo{S, T <: Tuple}
  x::S
  y::T
end

And I want to have

Tables.schema(foo::Foo{S, T}) where {S, T} = Tables.Schema(names(foo), Tuple{S, T...})

Of course T... is not working here, because it is a type.

view this post on Zulip Paul Bayer (Apr 07 2021 at 10:42):

f(::Type{S}, ::Type{T}) where {S,T<:Tuple} = Tuple{S,T.types...} should do

view this post on Zulip Paul Bayer (Apr 07 2021 at 10:43):

julia> f(String, Tuple{Int64, Int64, Float64})
Tuple{String, Int64, Int64, Float64}

view this post on Zulip Kwaku Oskin (Apr 07 2021 at 10:44):

Ah, this is amazing, thank you!

view this post on Zulip Kwaku Oskin (Apr 07 2021 at 10:45):

I was so close :-))


Last updated: Nov 06 2024 at 04:40 UTC