The NModel modeling framework includes a mechanism that makes it easy to
define custom structural types with a built-in base class called CompoundValue. This
data type is a useful building block for many kinds of data structured data values.
For example, a binary tree of integers could be defined in this way:
class IntTree : CompoundValue
{
public readonly int value;
public readonly IntTree left;
public readonly IntTree right;
public IntTree(int value, IntTree left, IntTree right)
{
this.value = value;
this.left = left;
this.right = right;
}
}
All fields of a type that derives from CompoundValue must be marked readonly.
This is because structural data types are immutable values, like strings and integers.
3 The string type isn??™t technically a value type in .NET terminology, but for simplicity we will
use that term because strings are immutable and use structural equality. We ignore the issue of
whether a value is stack-allocated.
Systems with Complex State 159
The data fields of a value are not variable locations of memory like typical instances
of class types.
Note that compound values, although they may be tree-structured, are by construction
not allowed to contain circular references.
The CompoundValue base class provides the necessary support for structural equality,
hash coding, and several other services required by the modeling framework.
Pages:
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226