julia> decomplexify{T}(::Type{Complex{T}}) = T
decomplexify (generic function with 1 method)
julia> type bar{S,T}
sum::S
sumsqr::T
function bar(s,ss)
if typeof(ss) != decomplexify(typeof(s))
error("Yaiks")
end
new(s,ss)
end
end
julia> bar{Complex{Float64},Float64}(1.5+2.0im,1.0)
bar{Complex{Float64},Float64}(1.5 + 2.0im,1.0)
julia> bar{S,T}(x::S,y::T) = bar{S,T}(x,y)
bar{S,T}
julia> bar(1.5+2.0im,1.0)
bar{Complex{Float64},Float64}(1.5 + 2.0im,1.0)
underlyingtype{T}(::Type{Complex{T}}) = T
type ComplexOf{S,T} sum::S sumsqr::T function ComplexOf{S,T}(s::S,ss::T) realtype = underlyingtype(S) if typeof(ss) != realtype throw( TypeError( :ComplexOf, "ComplexOf($s,$ss)\n\t\t", realtype, T )) end new(s,ss) endend
ComplexOf{S,T}(x::S, y::T) = ComplexOf{S,T}(x,y)
ComplexOf(1.5+2.0im, 1.0)
ComplexOf(5+2im, 3)
# force a type error by mixing the kinds of underlying typesComplexOf(5.0+1.5im, 2.0f0)
# presumably, you want to addfunction ComplexOf{T}(x::Vector{Complex{T}}) s = sum(x) ss = abs2(s) return ComplexOf(s,ss)end
test = ComplexOf([3.0+1.0im, 2.0-1.0im]);test.sum, test.sumsqr