[Cython] Feature proposal: conditional cast with None

Nikita Nemkin nikita at nemkin.ru
Fri Jun 7 13:24:38 CEST 2013


Hi,

Currently, conditional casts like <Name?>obj do not allow None values.
I have found it useful to allow None sometimes.
The syntax could be one of:
  * <Name or None?>obj
  * <Name? or None>obj
  * <Name??>obj

Use case (without this feature):

     tn_obj = self.any.non_trivial.nullable.expression()
     cdef TypeName tn
     if tn_obj is not None:
         tn = <TypeName?>tn_obj
         ...

Use case (with this feature):

     cdef TypeName tn = <TypeName or  
None?>self.any.non_trivial.nullable.expression()
     if tn is not None:
         ...

As you can see, without this feature, two local variables pointing to the  
same
object are required. This creates unnecessary confusion.

Implementation is trivial, all that is necessary is to pass "notnone=False"
flag from parser to TypecastNode to PyTypeTestNode.

What do you think?


Best regards,
Nikita Nemkin


More information about the cython-devel mailing list