On Apr 11, 2008, at 5:51 AM, Dag Sverre Seljebotn wrote:
>
>> This is (again) sort of an in-between C and Python question.  
>> "cdef" enters C
>> space, where block-scoping makes sense. However, we'd have to  
>> resolve all
>> sorts of weird semantic nonsense, such as:
>>
>>     cdef object i
>>     for i in range(10):
>>         cdef long i = i
>>         ...
>>
>> I don't feel like bothering with that...
>>
> Hmm. You're right. Even worse:
>
> if mytest:
>   cdef float x = 2
> else:
>   cdef int x = 2
> print x
>
> One would either have to use "C-like scope" for typed variables  
> (doesn't smell good), or know about execution paths and raise  
> compiler errors on the wrong scenarios (don't like that either). If  
> there are some simple rules to allow it in a few simple obvious  
> cases it would be good though, and just don't allow any possible  
> nonsense. Something like:
>
> syntax error: The variable "x" is used outside of the typed block.  
> Move the type declaration.
> syntax error: "x" has a type declared more than once within a  
> function.
>
> If one can do this check it should handle most cases, and can be  
> implement it simply by moving all the cdefs (splitting cdef and  
> assignment if necesarry) to the top of the function during compile.
>
> But perhaps better left for later.

This is really easy to change, as currently one can do

if mytest:
   x = 2
else:
   x = 2
print x

which is implicitly

if mytest:
   cdef object x = 2
else:
   cdef object x = 2
print x

and all variable declarations are moved to the top of the function at  
compile time anyways. One is not allowed to redeclare variables, so  
there would be a compile-time error. The only question is if it is  
misleading that the variables are not block-level (and shouldn't be,  
otherwise things will be very inconsistant and confusing to Python  
developers)?

- Robert

_______________________________________________
Cython-dev mailing list
Cython-dev@codespeak.net
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to