Parametric type without type parameters

I want to create a parametric type that would depend on a lot of parameters (e.g. 3 integer types)

struct MyType{T<:Integer, S<:Integer, U<:Integer}
  t::T
  s::S
  u::U
end

For the multiple dispatch I plan to use only T. What I don’t like is that all the parameter types are printed when I work with objects of type MyType. Also, implementing a custom Base.show for MyType is not enough, since if I use MyType as a type parameter in another type (e.g. Vector), all the 3 type parameters T, S, U will be showed again.

Is there a workaround for this? Since I use parameter T only for multiple dispatch, I was thinking about implementing MyType as follows:

struct MyType{T<:Integer}
  t::T
  s::S where S<:Integer
  u::U where U<:Integer
end

Is this implementation equivalent to the first one?

I think I remember GitHub - jonniedie/ConcreteStructs.jl: 🏩🏠🌆🏨🌇🏦 having something to hide parameters in types but I’m not sure

1 Like

Maybe a type alias would be sufficient?

julia> struct X{S, T}
           a::S
           b::T
       end

julia> X(1, 0x01)
X{Int64, UInt8}(1, 0x01)

julia> [X(1, 0x01)]
1-element Vector{X{Int64, UInt8}}:
 X{Int64, UInt8}(1, 0x01)

julia> const Y{T} = X{Int64, T} where {T}
Y (alias for X{Int64})

julia> X(1, 0x01)
Y{UInt8}(1, 0x01)

julia> [X(1, 0x01)]
1-element Vector{Y{UInt8}}:
 Y{UInt8}(1, 0x01)
3 Likes

Fields annotated with abstract types do not behave the same as fields annotated with concrete types, whether or not it happens via parameters. In this case, MyType2{Int} would be structured like MyType1{Int, Integer, Integer}.

1 Like