I think an even better definition would be
julia> type SelfRef
           objs::Array{SelfRef,1}
           function SelfRef()
               x = new()
               x.objs = SelfRef[]
               return x
           end
       end

julia> x=SelfRef()
SelfRef([])

julia> push!(x.objs,SelfRef())
1-element Array{SelfRef,1}:
 SelfRef([])

julia> x
SelfRef([SelfRef([])])


That way the first element of objs doesn't loop back around to itself.

On Wednesday, June 18, 2014 2:08:15 PM UTC-7, Stefan Karpinski wrote:
>
> Right, this definition is better:
>
> julia> type SelfRef
>            objs::Array{SelfRef,1}
>            function SelfRef()
>                x = new()
>                x.objs = [x]
>                return x
>            end
>        end
>
> julia> x = SelfRef()
> SelfRef([SelfRef(#= circular reference =#)])
>
>
> We don't enforce that constructors actually return the appropriate type.
>
>
> On Wed, Jun 18, 2014 at 4:58 PM, Luke Stagner <[email protected] 
> <javascript:>> wrote:
>
>> Thanks Stefan, although I feel like 
>> julia> x = SelfRef()
>> 1-element Array{SelfRef,1}:
>>  SelfRef([SelfRef(#= circular reference =#)])
>>
>> Should not return an array of type SelfRef. It should return just an 
>> instance of itself
>> As it stands, to get the behavior I want I need to call it like
>> julia> x=SelfRef()[1]
>> SelfRef([SelfRef(#= circular reference =#)])
>>
>> julia> x.objs
>> 1-element Array{SelfRef,1}:
>>  SelfRef([SelfRef(#= circular reference =#)])
>>
>> julia> push!(x.objs,SelfRef()[1])
>> 2-element Array{SelfRef,1}:
>>  SelfRef([SelfRef(#= circular reference =#),SelfRef([SelfRef(#= circular 
>> reference =#)])])
>>  SelfRef([SelfRef(#= circular reference =#)]) 
>>
>>
>> On Wednesday, June 18, 2014 12:24:34 PM UTC-7, Stefan Karpinski wrote:
>>
>>> 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