What are self-referential objects in the context of Julia?

Viewed 119

I am reading through the section on Constructors from the docs and the notion of self-referential objects was mentioned a few times. However, I did not seem to find what this term refers to in the context fo Julia.

1 Answers

Those are objects that can reference themselves (directly or indirectly). See the example below:

julia>  mutable struct Node
        parent::Node
        Node() = new()
        end;

julia> root = Node()
Node(#undef)

julia> root.parent = root
Node(Node(#= circular reference @-1 =#))


julia> function Node(parent::Node)
       n = Node()
       n.parent=parent
       n
       end;

julia> child = Node(root)
Node(Node(Node(#= circular reference @-1 =#)))

The reason that self-referential objects are of interest with respect to constructors and mutation is that a self-referential object cannot be constructed without mutation.

Related