Robert Bradshaw wrote:
> On May 24, 2010, at 4:50 AM, Stefan Behnel wrote:
>
>   
>> Hi,
>>
>> the current closures branch has limited support for inlined generator
>> expressions, i.e. you can write
>>
>>     any(x == 5 for x in some_seq)
>>
>> and Cython will generate an inlined loop for it that short circuits
>> properly on the first hit. To get this working correctly, I fixed the
>> scoping behaviour of the loop variable by giving generator expressions
>> their own scope, so that x won't leak into the surrounding scope in  
>> the
>> above example. However, this introduces a pretty annoying problem: you
>> can't type the loop variable any more. Any cdef declaration will  
>> only apply
>> to the surrounding scope, not to the scope of the generator  
>> expression.
>>
>> This may be acceptable in some cases where Cython can properly infer  
>> a type
>> for it, but in other cases, explicit typing is required to make the
>> expression work correctly and/or efficiently.
>>
>> Does anyone have an idea how we can let users provide an explicit  
>> type for
>> x in the above example? Maybe with a cast like this?
>>
>>     any(x == 5 for <int>x in some_seq)
>>
>> Any other ideas?
>>     
>
> That seems reasonable to me, and also goes well with, e.g., the  
> proposed syntax for typing varargs.
>   
How about some kind of builtin type for a typed iterator, e.g.

any(x == 5 for x in <iterable[int]>some_seq) # or cython.iterable

This has two advantages:

a) Using a call instead of a cast, it would work in pure Python mode
b) It is probably rather useful in a lot of other situations:

def f(iterable[int] x): ...
    cdef iterable[int] y = ...

def f(x: iterable[int]): ...

Or, perhaps there is some Python 3 Abstract Base Class stuff we could 
use for this concept?

Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to