Say I have a type hierarchy
abstract A
immutable B <: A end
immutable C <: A end
The constructor Afollows the factory pattern:
function A(x::Int)
if x > 0
B()
else
C()
end
end
It returns different subtypes based on input as expected. However, it is also type unstable, as I cannot find a way to make the return type be A.
So, is it bad to have a factory pattern here? Type instability affects only immutable types, not mutable types, since the latter is a reference type.
Do I need to choose a parametric type for this?
immutable D{T <: A}
type::T
end
function D(x::Int)
if x > 0
D(B())
else
D(C())
end
end
He's feeling a little bad.
Actually, how bad is it to have a type of unstable functions? Should I trade for better readability of the code?
Alternatively, should one typealias A Union{B,C}define instead ?