Chris Rackauckas
Chris Rackauckas

Reputation: 19132

Nested type parameters in a type definition

I am trying to create a type which nests, but need the lowest level as part of the type specification in order to be able to subtype from the right parmaeterized abstract type. However, the following errors:

immutable Type1{T} <: AbstractT{T}
  x::Vector{T}
end
immutable Type2{T,T2} <: AbstractT{T2}
  x::Vector{T{T2}}
end

Is there a good way to have that T2 for the specification?

Upvotes: 5

Views: 253

Answers (1)

mbauman
mbauman

Reputation: 31342

That sort of type computation isn't currently implemented. The standard workaround is something like this:

immutable Type2{T2,VTT2} <: AbstractT{T2}
  x::VTT2
end
Type2{T2}(x::Vector{Type1{T2}}) = Type2{T2, typeof(x)}(x)

You can further enforce the constraint in an inner constructor if you're really concerned about someone breaking the rules behind your back.

Upvotes: 9

Related Questions