Currently I am doing this like this. A kind of boxing.
class
ImmutableBox<T> where T : struct, new()
{
public readonly T state;
ImmutableBox(T state)
{
this.state = state;
}
}
struct
ExampleStateA
{
public string someField;
}
void
func1(ImmutableBox<ExampleStateA> passImmutableStateWithBox)
{
}
By keeping the instance of ImmutableBox<T>
, I can pass immutable object with pointer copy, and it's still easy to edit State
because State
is mutable. Also I gain a chance to optimize equality comparison to pointer comparison.