[fpc-pascal] Metaware

memsom memsom at interalpha.co.uk
Wed Jun 20 11:20:57 CEST 2007


> About three years ago, when this project was first proposed, I did find
> a white-paper written by someone at Metaware which outlined the
> internals of the yield().

And the code runs in a single thread or concurrently?

Looks like it would be nigh on impossible to translate the code as it
stands: for a start, the returning multiple resultants is not possible
(without using a type and therefore not likely to be generic)...

You might be able to break the code down as follows: create a stateful
class that represents the "iterator". Use the iteration pattern - the
class would have a "next" method that returns "true" when there is snother
iteration possible.  The "next" takes a method/function pointer, that
calculates the next item and stores the result. Sort of like how Borland
implemented the sort method in the TList class. Your "iterator" class
would need a stack and some kind of statefulness. You'd need some data
storage for the iteration result.

You could create a generic iterator in this way by doing something like:

type
  TIterationProc = function(target, stack: TObject): boolean of object;

  TIterator = class
  public
    stack: TObject; //holds data and state
    target: TObject; //is where the data goes
    operation: IterationProc; //moves to next iteration

    function Next(): boolean; //indirectly calls "operation"
  end;


A call to next then calls the TIterationProc (though this could be a
method of target I guess, if you want more concrete types to be used.)
Operation performs whatever the coder requires to generate the next step
and returns true when it is successful. Stack holds the, well, stack. You
could implement it however you saw fit really, I made it a TObject to
demonstrate it could be any type. Operation is coded to use Target to
store the iteration result. You'd assign the instance of whatever data
structire you want to use to it, and after calling "Next" the data is
updated.

You could then pretty it up by wrapping that mechanism into a class that
gives an interface that makes more sense. Wrapper would have the same
methods exposed as the iterator, but would encapsulate the fixing up the
internal data and typecasting it etc. The data could be exposed as
properties for example.

To be honest this is pretty noddy stuff, so I don't want to teach you to
suck eggs or anything, but it should be possible to do it.




More information about the fpc-pascal mailing list