Christopher Barker wrote: > Dag Sverre Seljebotn wrote: > > >> The argument against changing the default is simply that there is >> another option which is much closer to Python semantics (and IMHO more >> user friendly as the user don't have to care about declaring "not None" >> manually). >> >> Java has had decent "None" behaviour for ages using control flow >> analysis, so it can definitely be done. >> > > Java is a far more static language (though I guess we're talking about > static Cython anyway). > > >> As Robert says, it is just about >> moving the check (that should happen at some point anyway) to the point >> of first attribute access in a code path and then raise the exception >> there, just like Python would. >> > > This sounds simple enough, but would there really be a limited > performance hit? I'm a bit over my head here, but in something like this: > > cdef func(MyType x): > ... > > for i in a_really_big_loop: > if a_conditional: > do_something_with( x ) > > wouldn't you need to check if x were None inside that loop? Or could you > know that it _may_ be used, and therefor check before the loop? >
Yes, it gets messy -- but one can e.g. unroll the first iteration of the loop. Or simpler, accept the hit and document that you should do "assert x is not None" prior to a loop... All I know is that Java does it so it can't be too bad, but the compiler may have to be quite sophisticated in order to optimize everything that can be optimized. >> If you want to propose that >> >> cdef X x = None >> >> is also disallowed, then that's something else entirely. >> > > You mean that is allowed? I wouldn't have expected that! I guess I've > never tried it, nor been bitten by it, but yes, I'd think that would be > illegal. > Yes, and that's exactly why I want the above behaviour -- one can in principle get bitten by global variables as well as arguments (but Lisandro, Stefan and Greg would argue that the majority of the None-values come from user code in Python calling Cython code, so that they come in through arguments initially. I'm as concerned about just having a safeguard against my own bugs, and then global variables etc. are more important.). I coded the nonecheck directive for this reason, but the performance is hurt. The main problem in changing the behaviour is this: How to e.g. write the constructor of a cdef class if the typed attributes of self don't start off as None? (C++ gets around this with a custom functional-style syntax for just this purpose, requiring initialization of all fields prior to any procedural code). Also None is often convenient just to flag "not available" or similar. But then it is typically natural to explicitly test for None everywhere when using such a variable. Dag Sverre _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
