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?
A nested type is a type defined within the scope of another type, which is called the enclosing type. A nested type has access to all members of its enclosing type. For example, it has access to private fields defined in the enclosing type and to protected fields defined in all ascendants of the enclosing type.
In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they create an instance of the generic type.
Nested type, in C#, is a type declared within an existing class or struct. Unlike a non-nested type, which is declared directly within a compilation unit or a namespace, nested type is defined within the scope of the containing (or outer) type.
The type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or more parameters.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With