Type unstable & factory constructor
<p>Say, I have a type hierarchy</p>

<pre><code>abstract A
immutable B &lt;: A end
immutable C &lt;: A end
</code></pre>

<p>The constructor of <code>A</code> follows factory pattern:</p>

<pre><code>function A(x::Int)
    if x &gt; 0
        B()
    else
        C()
    end
end
</code></pre>

<p>It returns different subtypes based on the input as expected. However, it is also type unstable as I cannot find a way to force the return type to be <code>A</code>.</p>

<p>So, is it bad to have factory pattern here? Does the type instability only affects immutable types rather than mutable types, since the latter is reference type anyway.</p>

<p>Do I have to opt to the parametric type for this?</p>

<pre><code>immutable D{T &lt;: A}
    type::T
end

function D(x::Int)
    if x &gt; 0
        D(B())
    else
        D(C())
    end
end
</code></pre>

<p>It feels a bit bad.</p>

<p>Actually, how bad it is to have type unstable functions? Is is worthwhile to trade for better code readability?</p>

<p>Alternatively, should I define <code>typealias A Union{B,C}</code> instead?</p>