Type with arbitrary number of parameters

Hello!

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

Thank you in advance!

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.

2 Likes

Or have the only type parameter to be a tuple.

1 Like

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
julia> struct MyType{T<:Tuple}
       end

julia> MyType{Tuple{Int,Float64}}
MyType{Tuple{Int64, Float64}}

(Int,Float64) is not a type, it is a tuple of two types. Tuple{Int64, Float64} is a type.

1 Like

Great, thanks!

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?

I have no knowledge about that. Can you point me to the documentation of this change?

Not really applicable to types, but for is it’s values, but I meant to say that the same could be applied to types.

Anyway: allow nested combinations of (named)tuples, symbols, and bits as type parameters by JeffBezanson · Pull Request #46300 · JuliaLang/julia · GitHub

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:

julia> isbits(Int)
false

julia> isbits(Float64)
false

julia> isbits(1)
true

julia> isbits(1.0)
true

A tuple (1, 1.0) could would match them, but it is an entirely different thing.

Yeah, I figured. Thanks!!