> Thanks for your quick response! I have a couple questions/concerns about
> your implementation:
>
> - MyType(1, [2, 3], "xyz") fails requirement #2
MyType{T1<:Real, T2<:Real}(x::T1, v::Vector{T2}, s::String) =
(T=promote_type(T1,T2,Float64); MyType{T}(x, v, s))
If you want to keep Float16, etc this does not work. It will be at
least Float64 (or BigFloat).
If you need Float16 etc kept then it's going to be quite a bit more
verbose as arithmetic with pi promotes to Float64.
If you're fine with just Float64 and don't care about BigFloat then just
do:
immutable MyType
x::Float64
v::Vector{Float64}
s::String
...
end
> - MyType(1, [2, 10], "xyz") throws InexactError()
Should be fixed as well
> - Does the declaration s::String cause the ambiguities described in the FAQ
> here
> <http://docs.julialang.org/en/latest/manual/faq/#how-do-abstract-or-ambiguous-fields-in-types-interact-with-the-compiler>?
>
> More specifically, does MyType suffer from the same ambiguity
> as MyStillAmbiguousType in the FAQ?
I think this should only affect functions which actually use .s but not
.x or .v.
You can check this like so:
julia> f(a::MyType) = a.x+a.v[1]
f (generic function with 1 methods)
julia> f(aa)
3.0
julia> @code_typed f(aa)
1-element Array{Any,1}:
:($(Expr(:lambda, {:a}, {{},{{:a,MyType{Float64},0}},{}}, :(begin # none,
line 1:
return
(top(box))(Float64,(top(add_float))((top(getfield))(a::MyType{Float64},:x)::Float64,(top(arrayref))((top(getfield))(a::MyType{Float64},:v)::Array{Float64,1},1)::Float64))::Float64
end::Float64))))
This shows that Julia knows that the return type is Float64 (the last
bit of the returned thingy).
Compare this to:
julia> immutable MyType2
x
v::Vector
s::String
end
julia> b=MyType2(1, [2, 10], "xyz")
MyType2(1,[2,10],"xyz")
julia> g(a::MyType2) = a.x+a.v[1]
g (generic function with 1 method)
julia> g(b)
3
julia> @code_typed g(b)
1-element Array{Any,1}:
:($(Expr(:lambda, {:a}, {{},{{:a,MyType2,0}},{}}, :(begin # none, line 1:
return (top(getfield))(a::MyType2,:x) +
(top(arrayref))((top(getfield))(a::MyType2,:v)::Array{T,1},1)
end))))
No type annotation at the end of the output here, thus Julia cannot
infer the return type.