I’m toying with an undo-redo implementation, and I would like to have that an undo or redo item on its stack holds a reference to some Func<out TResult> where TResult is itself a Func<TResult>. The point would be that when Func is invoked from the undo stack, the return value is then stored on the redo stack (and vice-versa).
I’m not sure if it can be done hence this question.
The best I can come up with just won’t work as you can see here:
struct UndoRedoItem
{
public Func<Func<Func<Func<Func<Func<......>>>>>> UndoOrRedoFunc;
public string Description;
...
}
As you have discovered, having the undo Func return the redo Func directly doesn’t work, because the type argument required would grow infinitely. What you need to do is introduce a level of indirection, for example, by having the undo Func return the UndoRedoItem to be placed on the redo stack instead of just the redo Func: