Class ForwardReference<T>
- Namespace
- SodaFlow
- Assembly
- SodaFlow.dll
Constructs a value that can refer to itself during its own construction.
[PublicAPI]
public static class ForwardReference<T>
Type Parameters
TThe type of the value to make.
- Inheritance
-
ForwardReference<T>
- Inherited Members
Remarks
A cell loop gives a reference to a cell before the cell exists, and the cell that the reference means closes the loop. This is the same, but it removes the cell. The function gives one value and not a cell of changing values, and a constant cell closes the loop that the function gets. Thus, the reference resolves to that value and never changes. The two members here operate the same, and each closes a cell loop with a constant cell. That is what makes this the loop with one value: the reference resolves to the value the function produced and has nothing more to say. Use this for the cycle between two objects where each one needs the other one at its construction. Without this, the code must make one object incomplete and complete it after the other one. That needs a settable member, which is incorrect after the code builds the graph. For example:
Node node = ForwardReference<Node>.WithoutCaptures(
reference => new Node(new Child(reference.AsCell())));
The child holds a Cell<T> with no meaning until the call returns. It holds the finished node after that. Nothing has to be mutable. There is also no window where a half-built node is reachable, because no code can sample the reference before the construction function returns. That last point is the constraint worth remembering. The reference is a promise about what a value will be, and not the value. A read of it during the construction, with Sample<T>(Cell<T>) or with anything on it, asks a question with no answer now. The read throws an exception to say this. Everything here builds one transaction of its own, thus a caller can call this out of a transaction and in one.
Methods
- WithCaptures<TCaptures>(Func<LoopedCell<T>, (T Value, TCaptures Captures)>)
Constructs a value which can refer to itself, along with anything else worth keeping from its construction.
- WithoutCaptures(Func<LoopedCell<T>, T>)
Constructs a value which can refer to itself.