IDeepCloneable<T>#

A record that can be deep-copied. Input records, options classes, change sets and result classes in the package implement it, each with T set to its own type. You never create an IDeepCloneable<T> directly: call DeepClone() on a record such as ProjectData, TaskData or ScheduleResult.

Declaration#

namespace Plen;

public interface IDeepCloneable<out T>
{
    T DeepClone();
}

T is covariant (out).

Methods#

DeepClone#

T DeepClone()

Returns a deep copy of the record. The copy shares no mutable list or nested record with the original, so either one can be changed without affecting the other.

Returns - a new instance of T.

Immutable values aren't copied: Id instances and Moment values are shared, and that's safe because neither can change. A type can document a field it shares on purpose. For example, LevelingResult.DeepClone keeps the same Schedule object.

Generic constraint#

Changes<TAdd, TUpdate> requires both type arguments to implement IDeepCloneable<T>, so its own DeepClone() can copy every record in Add and Update:

public sealed class Changes<TAdd, TUpdate> : IDeepCloneable<Changes<TAdd, TUpdate>>
    where TAdd : IDeepCloneable<TAdd>
    where TUpdate : IDeepCloneable<TUpdate>

Example#

Change a copy of a result and leave the original as it was:

using Plen;

var data = new ProjectData
{
    Tasks = [new TaskData(1) { Duration = 2.0 }],
};

var result = Engine.Schedule(data, new SolveOptions());

ScheduleResult copy = result.DeepClone();
copy.Updates.Clear();

Console.WriteLine(result.Updates.Count); // unchanged