Nested Type Parameters in Type Definition

I am trying to create a type that nests, but I need the lowest level as part of the type specification in order to be able to subtype from the right parameterized 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 this T2 for specification?

+5
source share
1 answer

Such type computation is currently not performed. A standard workaround looks 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 optionally introduce a restriction in the internal constructor if you are really worried about who is breaking the rules behind your back.

+9
source

Source: https://habr.com/ru/post/1258560/


All Articles