I got another idea for how using C++ objects without no-arg constructors 
could work. It means enforcing C++ variable scoping through compile-time 
errors, but keeping Python syntax.

a) This gives compiler error: "CppObject has no no-arg constructor, but 
is used before assigned to"

   cdef CppObject obj
   obj.a = 3


b) This works:

   cdef CppObject obj
   if foo:
     obj = bar
     obj.func()
     del obj # note: introducing del, but only in a special case
   else:
     obj = bar2
     obj.asdf()
     del obj

c) Compiler error: "C++ object must be deleted before exiting block"

   cdef CppObject obj
   if foo:
     obj = bar
     obj.func()

d) Compiler error: "C++ object must be deleted within the same block"

   cdef CppObject obj
   if foo:
     obj = bar
     if True:
       del obj

e) All variables are deleted on exit

   cdef CppObject obj = bar
   obj.func()
   # end of function so don't require del

The transform necesarry to enforce these rules should not be too 
complicated. I don't think we need full analysis, just track assignments 
and deletions and usages on a per-name basis.

Finally, the C++ output is made by adding a block, i.e.

   cdef CppObject obj
   print 1
   obj = bar
   obj.func()
   del obj
   print 2

Results in

   print 1
   {
     CppObject obj = bar;
     obj.func()
   } # del
   print 2


Related: One must be able to call two-argument constructors on the stack 
as well. This could e.g happen like this:

cdef CppObject obj
print 1
obj = CppObject(1,2) # or, obj = (1, 2)

which would translate to

print 1
{
   CppObject obj(1,2);
   ...
}


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

Reply via email to