I traced a bug in some of my code down to some behavior that I don’t quite understand regarding type promotion. I’m not sure if the issue is with the MonteCarloMeasurements package I’m using or my general misunderstanding of the Julia language.
I am attempting to create an array of Particles
variables. Since some of the elements of the array are not uncertain, I was counting on the fact that creating a combined array of Particles
and Float64
will automatically promote the Float64
s to Particles
type. Here is an example of the behavior I was expecting:
julia> using MonteCarloMeasurements
julia> [3.2, 5.6±1.0]
2-element Array{Particles{Float64,500},1}:
3.2 ± 1.8e-15
5.6 ± 1.0
Notice the 3.2
became 3.2 ± 1.8e-15
, a Particles{Float64,500}
type. This is the behavior I expect and want to happen. Now if I define a struct with field val
and a helper function for retrieving that val
so I can call it’s vectorized form on an array, the array formed by the helper function is type Array{Real,1}
when I would expect it to be an Array{Particles{Float64,500}},1}
based off of the above promotion rule:
julia> struct Blah
val
end
val(arg::Blah) = arg.val
b = Blah(3.2)
c = Blah(5.6±1.0)
julia> val.([b, c])
2-element Array{Real,1}:
3.2
5.61 ± 1.0
This also happens if I use a list comprehension instead of a vectorized function call. If I create the array manually, though, it acts like I expect it to:
julia> [b.a, c.a]
2-element Array{Particles{Float64,500},1}:
3.2 ± 1.8e-15
5.61 ± 1.0
What am I missing?
(Also, as a side note: what is going on with 5.6 ± 1.0
becoming 5.61 ± 1.0
?)