Perhaps:
type RealorComplex{T1 <: Number, T2<:Real}
v::Vector{T1}
w::Vector{Complex{T2}}
function RealorComplex(v, w)
@assert T1 <: AbstractFloat || T1 <: Complex
new(v, w)
end
end
RealorComplex{T1,T2}(v::Vector{T1}, w::Vector{Complex{T2}}) =
RealorComplex{T1, T2}(v, w)
On Monday, June 13, 2016 at 9:31:06 PM UTC+2, Kuan Xu wrote:
>
>
>
> I'm trying to define a type which has two fields, the first of which can
> be either real or complex and the second is always complex.
>
> type RealorComplex{T<:Number}
> v::Vector{T}
> w::Vector{Complex{T}}
> end
>
> r = RealorComplex([1.0, 2.0], [1.0+1.0im, 2.0])
> t = RealorComplex([1.0+1.0im, 2.0], [1.0+1.0im, 2.0])
>
> The construction of r is totally fine but not that of t, since mismatch of
> the type. What is the best way to fix my ctor so that it can take either
> type of first argument .
>
> Hope I have made my question clear. Thanks.
>