Is `keyword_argument = Val{true}` good practice or an antipattern?

This (anti-)pattern makes a function, which I wrote, type-stable. What should I use as a better way?

I have a function that extract specific components from two plain vectors (state and parameters of an ODESystem-ODEProblem). The order of the components is only known after constructing the ODEProblem from the system, and I need a way to index it efficiently and conveniently by Symbols.

One version returns the plain vector and another version returns a LabelledArray to actually see the components. With a plain bool argument the result istype-unstable.

One alternative would be providing two differently named functions each returning its own result type.

Another alternative would require the use to explicitly call label_paropt himself, but he would need to pass the position-label-information (in ProblemParSetter) twice.

function get_paropt(ps::ProblemParSetter, u0::Vector, p::Vector; label::Val{LABEL}=Val(false)) where LABEL
    v = [(first(t) == :par) ? p[last(t)] : u0[last(t)] for t in ps.optinfo]
    LABEL ? label_paropt(ps,v) : v
end

function label_paropt(ps, popt::Vector); LArray{paroptsyms(ps)}(popt); end
1 Like