This works:

julia> type SelfRef
           objs::Array{SelfRef,1}
           function SelfRef()
               x = new()
               x.objs = [x]
           end
       end

julia> x = SelfRef()
1-element Array{SelfRef,1}:
 SelfRef([SelfRef(#= circular reference =#)])


A couple of points:

The type Array{SelfRef,0} is a zero-dimensional array of SelfRef objects,
not a zero-length one-dimensional array of SelfRef objects. The type of a
one-dimensional array of SelfRef objects is Array{SelfRef,1} – aka
Vector{SelfRef}.

The first argument to the Array constructor is always a type, whereas x is
an instance of type SelfRef. Writing [x] gives a vector of type SelfRef
containing just x.



On Wed, Jun 18, 2014 at 4:32 AM, Luke Stagner <[email protected]> wrote:

> Hello all,
>
> I know it is possible to have a self-referential type like the following
>
> type SelfRef
>    obj::SelfRef
>    SelfRef() = (x=new(); x.obj=x)
> end
>
> what I want to do is have obj be an array of type SelfRef so I can make a
> tree-like structure. I tried the following
>
> type SelfRef
>    obj::Array{SelfRef,0}
>    SelfRef() = (x=new(); x.obj=Array(x,0))
> end
>
> but that throws the following error (on nightly)
> julia> a=SelfRef()
> ERROR: no method Array{T,N}(SelfRef)
>  in SelfRef at none:3
>
> Is there anyway I can make this work?
>

Reply via email to