Your right….
["param" => 2.0] is of type Dict{ASCIIString,Float64} which is not a
subtype of Dict{ASCIIString,Number}
You can use parametric types to solve your problem as follows:
type MyType{T}
data::Array{Float64, 1}
meta::Dict{ASCIIString, T}
N::Int64
end
MyType{T}(data::Array{Float64, 1}, meta::Dict{ASCIIString, T}) = MyType(data,
meta, length(data))
Notice, that you also had an error when passing size(data) to MyType since
size(data) returns a tuple rather than an Int64. I changed it to
length(data) in the above example.
On Monday, May 19, 2014 2:53:38 PM UTC-7, Davide Lasagna wrote:
Hi,
>
> I am starting with julia and stumbled upon this issue. Say I want to
> define this composite type
>
> type MyType
> data::Array{Float64, 1}
> meta::Dict{ASCIIString, Number}
> N::Int64
> end
> where the data field will store some floats, meta will be a dict of
> metadata (from a simulation), and N is some other number storing the size
> of data (just a silly example).
>
> Say I define another constructor as:
> MyType(data::Array{Float64, 1}, meta::Dict{ASCIIString, Number}) = MyType(
> data, meta, size(data))
>
>
> If I then create an instance with the default constructor I get the
> expected behaviour:
> MyType(zeros(Float64, 5), ["param"=>2], 5)
>
> "MyType([0.0,0.0,0.0,0.0,0.0],["param"=>2], 5)"
>
>
> However, if I use the additional constructor I obtain a
> MyType(zeros(Float64, 5), ["param"=>2.0])
>
> "no method MyType(Array{Float64,1}, Dict{ASCIIString,Float64})
> while loading In[4], in expression starting on line 1"
>
>
>
> As far as I understand, by reading the documentation regarding parametric
> composite types, the error is raised because Dict{ASCIIString, Float64}
> is not a subtype of Dict{ASCIIString, Number}.
>
> What is the approach to cope with this problem?
>
> Thanks,
>
> Davide
>
>