Stefan Behnel | 3 Jul 2012 06:58
Picon
Favicon

[Cython] Cython methods for C/C++ types

Hi,

the discussion on allowing for automatic user provided ways to coerce
between Python types and C++ types got me thinking that this might hide a
rather interesting general feature: methods for low-level types. I faintly
remember that this idea has come up in our discussions before, but here's a
draft CEP for it:

http://wiki.cython.org/enhancements/ctypemethods

Basically, it would allow writing this in .pxd files:

"""
cdef extern from "...":
    cdef cppclass MyClass:
        cdef int cython_method_here(self):
            return 1

ctypedef double mydouble:
    cdef double cython_method_here(self):
        return self ** 2

cdef struct mystruct:
    int x, y

    cdef int cython_method_here(self):
        return self.x + self.y

cdef union myunion:
    int a
(Continue reading)

mark florisson | 3 Jul 2012 12:12
Picon
Gravatar

Re: [Cython] Cython methods for C/C++ types

On 3 July 2012 05:58, Stefan Behnel <stefan_ml@...> wrote:
> Hi,
>
> the discussion on allowing for automatic user provided ways to coerce
> between Python types and C++ types got me thinking that this might hide a
> rather interesting general feature: methods for low-level types. I faintly
> remember that this idea has come up in our discussions before, but here's a
> draft CEP for it:
>
> http://wiki.cython.org/enhancements/ctypemethods
>
> Basically, it would allow writing this in .pxd files:
>
> """
> cdef extern from "...":
>     cdef cppclass MyClass:
>         cdef int cython_method_here(self):
>             return 1
>
> ctypedef double mydouble:
>     cdef double cython_method_here(self):
>         return self ** 2
>
> cdef struct mystruct:
>     int x, y
>
>     cdef int cython_method_here(self):
>         return self.x + self.y
>
> cdef union myunion:
(Continue reading)

mark florisson | 3 Jul 2012 12:15
Picon
Gravatar

Re: [Cython] Cython methods for C/C++ types

On 3 July 2012 05:58, Stefan Behnel <stefan_ml@...> wrote:
> Hi,
>
> the discussion on allowing for automatic user provided ways to coerce
> between Python types and C++ types got me thinking that this might hide a
> rather interesting general feature: methods for low-level types. I faintly
> remember that this idea has come up in our discussions before, but here's a
> draft CEP for it:
>
> http://wiki.cython.org/enhancements/ctypemethods

I see this CEP also mentions overriding C++ methods. But I think if it
doesn't support virtual methods, then overriding base class methods by
calling another function is pretty horrible.

> Basically, it would allow writing this in .pxd files:
>
> """
> cdef extern from "...":
>     cdef cppclass MyClass:
>         cdef int cython_method_here(self):
>             return 1
>
> ctypedef double mydouble:
>     cdef double cython_method_here(self):
>         return self ** 2
>
> cdef struct mystruct:
>     int x, y
>
(Continue reading)

Stefan Behnel | 3 Jul 2012 12:49
Picon
Favicon

Re: [Cython] Cython methods for C/C++ types

mark florisson, 03.07.2012 12:15:
> On 3 July 2012 05:58, Stefan Behnel wrote:
>> the discussion on allowing for automatic user provided ways to coerce
>> between Python types and C++ types got me thinking that this might hide a
>> rather interesting general feature: methods for low-level types. I faintly
>> remember that this idea has come up in our discussions before, but here's a
>> draft CEP for it:
>>
>> http://wiki.cython.org/enhancements/ctypemethods
> 
> I see this CEP also mentions overriding C++ methods. But I think if it
> doesn't support virtual methods, then overriding base class methods by
> calling another function is pretty horrible.

Right, I didn't think that part through. If we don't know the real type at
compile time and get a subtype at runtime then we'll call the wrong code.

I agree that that's not a desirable feature.

Stefan

Gmane