2nd is much better. But you can even write your `kind` function using
dispatch:
kind{T}(g::GMM{T,Matrix{T}}) = :diag
kind{T}(g::GMM{T,Vector{Matrix{T}}}) = :full
See the FAQ for more info about containers with abstract fields (especially the
part about inner constructors).
--Tim
On Tuesday, October 28, 2014 02:03:57 AM David van Leeuwen wrote:
> Hello,
>
> For a long lime I've been having a bad feeling for having a Union in a
> composite type, as in
>
> type GMM{T:<FloatingPoint}
> ...
> kind::Symbol
> μ::Matrix{T}
> Σ::Union(Matrix{T},Vector{Matrix{T}})
> ...
> end
>
> where the choice in `Σ` depends on whether `kind==:diag` or `kind==:full`.
> I have the feeling it is better to have defined something along the lines
> of
>
> typealias DiagCov{T} Matrix{T}
> typealias FullCov{T} Vector{Matrix{T}}
>
> type GMM{T:<FloatingPoint,CT}
> ...
> μ::Matrix{T}
> Σ::CT
> end
>
> function kind(g::GMM)
> T = eltype(g.μ)
> if isa(g.Σ, FullCov{T})
> return :full
> else
> return :diag
> end
> end
>
>
> In the second case, the `kind` is inferred from the type instance, rather
> than stored as a type variable.
>
> What is in your expert's opinion a better way of defining such a type?
>
> Thanks,
>
> ---david