I tried with z::Type{T}, that didn't change. sorry if this is hard to
understand: in that line
x = [z("hi") for i=1:n]
I want to call the constructor for MySubType, which here I hoped would be
passed through in the variable z. hence z("hi").
On Wednesday, 11 June 2014 17:37:34 UTC+1, John Myles White wrote:
>
> I don’t see how z::T and z(“hi”) can both make sense. Do you perhaps mean
> z::Type{T}?
>
> — John
>
> On Jun 11, 2014, at 9:34 AM, Florian Oswald <[email protected]
> <javascript:>> wrote:
>
> Sorry guys but I have to come back to this. My problem is slightly more
> involved than the initial example, in the sense that in the array I want to
> construct another custom type (let's call it my subtype here):
>
> abstract MyAbstract
>
> type MySubType <: MyAbstract
> field1 :: ASCIIString
> function MySubType(x)
> new(x)
> end
> end
>
>
> type Myt{ T<:MyAbstract}
> chain :: Array{T,1}
> function Myt(n,z::T)
> x = [z("hi") for i=1:n]
> new(x)
> end
> end
> Myt{T}(n::Integer,z::T) = Myt{T}(n,z)
>
> trying to construct Myt gives
>
> Myt(10,MySubType)
> ERROR: type: Myt: in T, expected T<:MyAbstract, got Type{DataType}
>
> How would you do this?
> thanks!
>
>
> On Wednesday, 11 June 2014 17:00:13 UTC+1, Tim Holy wrote:
>>
>> Normally Julia creates the inner and outer constructors for you, but if
>> you
>> supply your own inner constructor you need to explicitly supply the outer
>> one
>> too:
>>
>> julia> 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
>>
>> julia> Myt(5, 3.2f0)
>> ERROR: no method Myt{T<:Real}(Int64, Float32)
>>
>> julia> Myt{T}(n::Integer, z::T) = Myt{T}(n,z)
>> Myt{T<:Real} (constructor with 1 method)
>>
>> julia> Myt(5, 3.2f0)
>> Myt{Float32}(5,Float32[3.2,3.2,3.2,3.2,3.2])
>>
>>
>> The declaration of the outer constructor can be a bit confusing: you
>> might
>> say, the LHS and RHS look almost identical, so why do you need it? Here's
>> the
>> translation: "given an integer and a value of type T<:Real, construct a
>> Myt{T}
>> with those values." You could instead have declared something like
>>
>> Myt(n::Integer, z::FloatingPoint) = Myt{Float64}(n,float64(z)),
>>
>> which you'll see gives you considerable flexibility.
>>
>> --Tim
>>
>> On Wednesday, June 11, 2014 08:36:51 AM Florian Oswald 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]
>> > > <javascript:>> 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
>>
>>
>