I’m writing an implementation of a virtual machine in C#, and I need to implement the VM’s stack, which can contain two types of entry – return entries or backtrack entries. What is the best way of implementing this?
I’m currently using a base type, as follows:
class StackEntry { } class Return : StackEntry { uint pc; } class Backtrack : StackEntry { uint pc; object backtrack; } Stack<StackEntry> stack;
This works OK, but the resulting type testing and downcasting feels clumsy.
Is there a better way of handling this type of construction?
I’m having a hard time imagining how you’re going to use this, but the basic answer is that you use a single type with a default operation for post-pop processing
Does that make sense?