How can I instantiate immutable mutually recursive objects?

后端 未结 4 1999
闹比i
闹比i 2020-12-30 02:25

I have an immutable recursive type:

public sealed class Foo
{
    private readonly object something;
    private readonly Foo other; // might be null

    pu         


        
4条回答
  •  滥情空心
    2020-12-30 03:06

    It is definitely impossible using write-once immutability. Let me explain why. You can set fields' values only at constructor. So if you want a to refer to b you have to pass reference to b to a's constructor. But b will be already frozen. So the only option is to instantiate b in a's constructor. But this is impossible, because you can't pass reference to a, because this is invalid inside constructor.

    From this point popsicle immutability is the simplest and most elegant solution. Another way is to create static method Foo.CreatePair that will instantiate two objects, set cross reference and return frozen object.

    public sealed class Foo
    {
        private readonly object something;
        private Foo other; // might be null
    
        public Foo(object something, Foo other)
        {
            this.something = something;
            this.other = other;
        }
        public object Something { get { return something; } }
        public Foo Other { get { return other; } private set { other = value; } }
    
        public static CreatePair(object sa, object sb)
        {
            Foo a = new Foo(sa, null);
            Foo b = new Foo(sb, a);
            a.Other = b;
            return a;
        }
    }
    

提交回复
热议问题