Prakhar Goel, > Doesn't this just punt on how CdefFunction works? Feels like we're > back to AST re-writing. Indeed, internally it rewrites the AST, of course. I think that's the simplest way to implement it. But it doesn't mean the user that write the compile-time code would have to be aware of the AST at all. I mean, if you have a good use-case for it, then I'm ok with allowing the compile-time code manipulating it. But I'd really prefer this not be the main use of the compile-time execution feature.
Greg Ewing, > On 26/02/21 3:21 pm, Celelibi wrote: > > def twice(f): > > fun = CdefFunction(... use f ...) > > return fun > > > > foo = CdefFunction(...) > > ast.append(AstCdefFunction("foo", twice(foo))) > > > > I think this might even be doable without having to even detect the > > closure in cython. We'd just have to let python perform the name lookup. > > The cdef function created inside twice() is going to have > to be some kind of closure, because it needs access at run > time to the particular f that was passed to twice() at > compile time. Yes, conceptually it's a closure. But not implemented as one. Since the object `fun` (whose type is CdefFunction) is *not* a function, it is not a closure in the usual sens. It's just some python object having a reference to another object. The only new feature might be to declare at the top-level the functions that are referenced by the CdefFunction object that reach the top-level. In short, the apparent closures are resolved during the compile-time execution. As Prakhar Goel pointed out, the AST of `fun` would hold a reference to the AST of `foo`. The compile-time execution of the `twice` example would produce an AST similar to that we would get by parsing the following code. cdef _unique_name_for_original_foo(x): # Something math-y cdef foo(x): return _unique_name_for_original_foo(2*x) Honestly, I would love to make a prototype as think there's really no major obstacle, yet many seem to think there is. Unfortunately, I don't think I'll have enough time until at least several weeks. Best regards, Celelibi _______________________________________________ cython-devel mailing list cython-devel@python.org https://mail.python.org/mailman/listinfo/cython-devel