Iteration madness

Kirk McDonald kirklin.mcdonald at gmail.com
Mon Jul 3 01:28:32 PDT 2006


So I'm working away at getting Pyd to automatically wrap a class's 
operator overloads, and it's going great. Until, that is, I get to opApply.

Oh yeah.

Python does iterators differently. First, you call PyObject_GetIter on 
an object, which returns an iterator object. Then you just keep calling 
PyIter_Next on the iterator object until it returns null.

It turns out that wrapping opApply with this sort of interface is 
essentially impossible. Long story short, it requires static closures in D.

More specifically, it requires functionality equivalent to Python's 
"yield" keyword. This is a keyword that causes a function to act like a 
generator. As the generator is iterated over, it just keeps running the 
function, using the results of the yields as the iterator values. This 
is a classic Python way of creating an iterator for a class.

So, in the meantime, wrapped opApply functions will have to be more 
literally translated as functions with a callback argument.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d mailing list