Now that you’ve fixed the bug in your original example, I think you're hitting
a confusing issue about how parametric inner constructors work. See below:
julia> type MyT{T <: Real}
n::Int
chain::Array{T,1}
function MyT(n, z::T)
x = [z for i in 1:n]
new(n, x)
end
end
julia> MyT
MyT{T<:Real} (constructor with 0 methods)
julia> methods(MyT)
# 0 methods for generic function "MyT":
julia> MyT{T <: Real}(n, z::T) = MyT{T}(n, z)
MyT{T<:Real} (constructor with 1 method)
julia> MyT(10, 3.0)
MyT{Float64}(10,[3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0])
Learning how to write a constructor for a parametric type is probably the most
confusing thing about Julia. As you can see, the inner constructor you’ve
defined doesn’t have a corresponding outer constructor, so you have to define
one manually.
There’s a description of this in the manual if you’d like more info:
http://julia.readthedocs.org/en/latest/manual/constructors/#parametric-constructors
— John
On Jun 11, 2014, at 8:36 AM, Florian Oswald <[email protected]> wrote:
> hum, good point.
> however, the same applies if I change this:
>
> type Myt{ T<:Real}
> n :: Int64
> chain :: Array{T,1}
> function Myt(n,z::T)
> x = [z for i=1:n]
> new(n,x)
> end
> end
>
> or is that not what you meant? I basically want to fill array "chain" with
> several copies of type T.
>
> On Wednesday, 11 June 2014 16:13:47 UTC+1, John Myles White wrote:
> Your type’s definition doesn’t seem to depend upon T in any way. Keep in mind
> that the type is really just the first two lines of your code:
>
> type Myt{T <: Real}
> n::Int64
> chain::Array
> end
>
> The constructor isn’t part of the type itself, so the dependency on T needs
> to occur in the type.
>
> — John
>
> On Jun 11, 2014, at 8:04 AM, Florian Oswald <[email protected]> wrote:
>
>> Hi,
>>
>> I'm trying to understand why this is not working:
>>
>> type Myt{ T<:Real}
>> n :: Int64
>> chain :: Array
>> function Myt(n,z::T)
>> x = [z for i=1:n]
>> new(n,x)
>> end
>> end
>>
>> i get the error
>> julia> Myt(10,18.0)
>> ERROR: no method Myt{T<:Real}(Int64, Float64)
>>
>> I thought that was very similar to the point example on the manual?
>>
>> type Point{T<:Real}
>>
>>
>> x::T
>>
>>
>> y::T
>>
>>
>>
>> Point(x::T, y::T) = new(x,y)
>> end
>>
>> thanks
>>
>