On Oct 1, 2009, at 7:17 AM, Neal Becker wrote:

> I need to code some cython functions which will be instantiated for  
> several
> different data types.  Very much like what c++ templates are used for.
>
> What approaches can be used to do this?
>
> (I'm guessing some kind of macro text processing.  Does cython use  
> cpp?)
>
> Now suppose in general I need a little more intelligence.  For  
> example,
> suppose I have a function that is templated on a single datatype.   
> Suppose
> some types internal to that function depend in a (possibly  
> nontrivial way)
> on that datatype.  Same approach?
>
> How about a function templated on multiple datatypes?

Somewhat hackish is to do this with a pxi.

Define a pxi using a dummy type, e.g.

cdef class Wrapper:
     cdef TYPE value
     cdef set(self, TYPE value):
         self.value = value
     cdef TYPE get(self):
         return self.value
     ...

Then make several pyx/pxd files that do

ctypedef TYPE double
include "wrapper.pxi"

And voila, "templates."

Of course, eventually we'll want to have a much nicer way of doing  
this, e.g. being able to template cdef classes like you can in C++  
(though I don't think we want to go with the full template  
metaprograming model they have).

- Robert

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

Reply via email to