Hi Tim and all,

really appreciate your patience. Here is the final outcome of my learning 
process, with the conclusion that I'll just use Array{MySubType}, i.e. Myt 
is kind of redundant. Neverthless, this version here worked - i'll post 
that here in the unlikley case someone finds it useful. i don't actually 
call the constructor for MySubType, just copy it. So it's really just 
getting an array. Anyway, thanks a lot for chipping in!

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 for i=1:n]
        new(x)
    end
end
Myt{T}(n::Integer,z::T) = Myt{T}(n,z)

function newType(whichtype,x)
    if isa(whichtype,MySubType)
        return MySubType(x)
    else
        println("no other types")
    end
end

Myt(3,MySubType("hi"))
Myt{MySubType}([MySubType("hi"),MySubType("hi"),MySubType("hi")])




On Wednesday, 11 June 2014 18:13:04 UTC+1, Tim Holy wrote:
>
> On Wednesday, June 11, 2014 09:34:52 AM Florian Oswald wrote: 
> > type MySubType <: MyAbstract 
> >     field1 :: ASCIIString 
> >     function MySubType(x) 
> >         new(x) 
> >     end 
> > end 
>
> I would strongly recommend getting rid of the inner constructor. It 
> doesn't do 
> anything that the default one doesn't do, and you've just caused Julia to 
> skip 
> creating an outer constructor for you. In most code, inner constructors 
> are 
> relatively rare and should be provided only if you have a specific need 
> for 
> them. 
>
> > Myt(10,MySubType) 
> > ERROR: type: Myt: in T, expected T<:MyAbstract, got Type{DataType} 
>
> The interpretation of that error is the following: you're passing z, which 
> you 
> declare must be an object for which isa(z, MyAbstract) is true. But 
> isa(MySubType, MyAbstract) is false. You need to pass an instance (e.g., 
> MySubType("hi")) rather than the type itself. 
>
> Once you realize that z must be an instance of type T, you'll quickly 
> realize 
> that your z("hi") in the inner constructor of Myt is another error. It 
> should 
> just be z. 
>
> As John says, the alternative is to delcare the Myt inner constructor as 
> Myt(n,z::Type{T}) and then try to build it as you did. But I doubt that 
> will 
> give you what you want. 
>
> --Tim 
>
>

Reply via email to