[Prev][Next][Index][Thread]

Re: simple method dispatch question



On Fri, Nov 10, 2000 at 05:15:01PM -0500, Michael T. Richter wrote:

> This looks interesting, but it seems to me to obfuscate semi-trivial (and
> very conventional) conditional logic using the dispatch mechanism.  Doesn't
> this tend to make write-only code outside of trivial examples?

Not at all.  Here's a not-quite-as-trivial example: consider writing a
virtual machine...

define function run(program)
  for (opcode in program)
    run-instruction(opcode);
  end for;
end function;

define method run-instruction(instruction == #add)
  push(*stack*, pop(*stack*) + pop(*stack*));
end method;

define method run-instruction(instruction == #multiply)
  push(*stack*, pop(*stack*) * pop(*stack*));
end method;

...
...
...

This is certainly tons cleaner than a traditional switch statement,
just about as fast,  you can split opcode implementations up into
multiple files, and you can even add opcode extensions later just by
defining new methods.

-jeff


Follow-Ups: References: