Parametric Type Usage

Suppose I have a type PieceWiseAffineFunction with a number of segments.
I want to convert (approximate) an instante with m segments into an instant with n segments. (n<m)

type PieceWiseAffineFunction
    k::Vector{Float64}
    d::Vector{Float64}
end

If I have an instant with 3 segments k and d are 3 long.

p = PieceWiseAffineFunction([1.0,2.0,3.0], [4.0,5.0,6.0])

Is there a way to use parametric types to achieve this e.g.:

q = PieceWiseAffineFunction{2}(p)

or is this bad practice and it I should simply use an outer constructor?

function PieceWiseAffineFunction(p::T, num_seg::Int) where {T<:PieceWiseAffineFunction}
   # TODO:
end

q = PieceWiseAffineFunction(p, 2)

Yeah, I would agree with both of those statements. Using {2} to pass something which is not a type parameter would be pretty surprising and unusual. An outer constructor would be easy to implement and easy to read.

1 Like