Hi Robert,

On 2014-05-14, Robert Bradshaw <rober...@math.washington.edu> wrote:
> Another option is to do code generation, e.g. with one of the (many)
> Python templating libraries out there.

You mean some program will write Cython code for me, Cython will
write C code for me, and gcc will do the rest? Has this approach been
used in Sage before?

Perhaps related with this: One could create a collection of "templated"
cpdef functions, using fused types (see below), and then have a metaclass,
that puts the appropriate specialisation of the cpdef functions into a
Python class' dict.

The following may be similar. Do you think that the approach makes sense?
It is what I have outlined at #15820, and produces something like methods
whose implementation is indexed by an extenion class (and is something
that a metaclass would doubtlessly be able to do automatically):

cdef class Bar_int
cdef class Bar_str

cdef fused Bar:
    Bar_int
    Bar_str

def foo_(Bar self):
    <code that is special-cased for Bar_int and Bar_str.
     It is even able to access cdef attributes of Bar_int and Bar_str
     as the .data attribute below!>

cdef class Bar_str:
    cdef str data
    foo = foo_[Bar_str]

cdef class Bar_int:
    cdef int data
    foo = foo_[Bar_int]

Then, if you have an instance b of either Bar_int or Bar_str then b.foo()
calls the version of foo_ that belongs to the respective class.

Problems:
- Defining "cpdef Bar foo_(Bar self)" is possible, but then there is
  an error in the line "foo = foo_[Bar_...]". Why? I thought that a cpdef
  function can be treated as a Python object?
- The code above puts foo_ into Bar_int.__dict__['foo']. Do you see a
  way to put it into a c(p)def attribute rather than into the class' dict?
- Note that above I assign the "templated" function named "foo_" to an
  attribute named "foo". Unfortunately, it seems impossible to give both
  the same name: "foo_ = foo_[Bar_int]" fails with the complaint that
  Bar_int has no attribute "foo_" (well, of course it hasn't, I just try
  to create such attribute!)

Best regards,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to