You don't need the {T} on the constructor; being inside the type definition
gives you access to that T.
Your call to the constructor needs to have the type parameter explicitly.
The type parameter here indicates which type you want to create.
~~~
MyType{Float64}(zeros(Float64, 5), ["param"=>2.0])
~~~
Alternately, adding an outer constructor definition will make your call
work:
~~~
MyType{T}(data::Array{Float64,1}, meta::Dict{ASCIIString,T}) =
MyType{T}(data,meta)
~~~
(this just calls the inner constructor you already wrote.)
On Tue, May 20, 2014 at 2:15 PM, Davide Lasagna <[email protected]>wrote:
> Ok, thanks. However, in my actual problem i have to use an inner
> constructor, something like
>
> type MyType{T}
> data::Array{Float64, 1}
> meta::Dict{ASCIIString, T}
> N::Int64
>
> function MyType{T}(data::Array{Float64, 1}, meta::Dict{ASCIIString, T})
> new(data, meta, length(data))
> end
>
> end
>
> This results in the same kind of "no method" error, like:
>
> MyType(zeros(Float64, 5), ["param"=>2.0])
>
> Why this? Any solution?
>
> Thanks,
>
> Davide
>
>
>
> On Monday, May 19, 2014 11:55:24 PM UTC+1, Ethan Anderes wrote:
>>
>> 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
>>>
>>>