I am struggling to create a type with an arbitrary number of parameters, similarly to what Tuple does.
Ideally, I would use it in functions like this:
julia> function f(type_list, string_list)
map(zip(type_list, string_list)) do (t, s)
parse(t,s)
end
end
julia> f([Int,Float64], ["1","3"])
2-element Vector{Real}:
1
3.0
That type list could ideally come from the type, so that I could do something like:
function f(type_with_parameters{T...}, string_list)
map(zip(T, string_list)) do (t, s)
parse(t,s)
end
end
Tuple is a special builtin; it’s ability to have a variable number of type parameters is not something you can replicate in a new type. You can however use a tuple directly for what you’re doing.
Yeah, I thought so. However, I don’t see how I can do that:
julia> struct MyType{T<:Tuple}
end
julia> MyType{(Int,Float64)}
ERROR: TypeError: in Type, in parameter, expected Type, got a value of type Tuple{DataType, DataType}
Stacktrace:
[1] top-level scope
@ REPL[2]:1
julia> struct MyType2{T} where T <: Tuple
end
ERROR: syntax: invalid type signature around REPL[4]:1
Stacktrace:
[1] top-level scope
@ REPL[4]:1
julia> struct MyType2{T} where {T <: Tuple}
end
ERROR: syntax: invalid type signature around REPL[5]:1
Stacktrace:
[1] top-level scope
@ REPL[5]:1
julia> struct MyType2{T::Tuple}
end
ERROR: syntax: invalid variable expression in "where" around REPL[6]:1
Stacktrace:
[1] top-level scope
@ REPL[6]:1
julia> struct MyType2{::Tuple}
end
ERROR: syntax: invalid variable expression in "where" around REPL[7]:1
Stacktrace:
[1] top-level scope
@ REPL[7]:1
As a side note, since last Julia version that tuples have wider support as parameters for types, shouldn’t my version of it (using tuples instead of the type tuple with parameters) work?
No, because T <: Tuple means “a type T subtyping Tuple”, not “an object whose type is a subtype of Tuple”. Your restriction requires a type - you cannot express the constraint “an object that is of a specific type” in a type parameter.
The important distinction is that an object and its type are not the same thing.
This change only applies for (named) tuples that have inside isbits objects and/or Symbols and/or (named) tuples following this same restriction. Your (Int,Float64) does not match these per-requisites: