Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested type parameters in a type definition

Tags:

types

julia

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?

like image 564
Chris Rackauckas Avatar asked Oct 20 '16 17:10

Chris Rackauckas


People also ask

What is a nested type?

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.

What is type parameters in generics?

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.

What is a nested type C#?

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.

What is class type parameter?

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.


1 Answers

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.

like image 162
mbauman Avatar answered Oct 17 '22 18:10

mbauman