What are valid type parameters?

I am trying to stuff quite a bit of information into parameters of a type, in the hope that dispatching on this will create efficient code. Doing this, I ran into the question of what is allowed, exactly.

Is the following correct: a value V is valid for a type parameter iff either

  1. isbits(V)
  2. isa(V, Symbol)
  3. isa(V, Type)
    Is there anything else?

To make things concrete, what I am trying to encode as type parameters are a return type, and a Vector{Pair{Int,Type}}. The latter does not work, but I can split the pairs, ending up two vectors, which works.

Also Tuple of Symbol and isbits type.

Encode return type in parameter is fine (like convert) but you shouldn’t try to “help” inference this way.

1 Like

Thanks. I am not “helping” type inference, I am encoding all the information in type parameters (it does not exist anywhere else). I got the idea from a code example which converts parser spec strings to symbols, and puts them in a type parameter (can’t find the link now).

Another question I ran into is the following: given a Tuple{...}, how do I extract the type parameters? Eg from Tuple{Int,Float64}, how would I get the [Int,Float64]?

EDIT:

xx(T::Type) = map(name->fieldtype(T,name), fieldnames(T))

works. But I have a feeling I probably should not be doing this :smiley:

julia> [Tuple{Int,Float64}.parameters...]
2-element Array{DataType,1}:
 Int64
 Float64
1 Like

neat. another question: how could I have discovered this? I could not find it in the manual, and dump does not give anything meaningful for Tuples.

julia> typeof(Tuple{Int,Float64})
DataType

julia> fieldnames(DataType)
17-element Array{Symbol,1}:
[...]
:parameters
[...]