[Cython] New function (pointer) syntax.

Stefan Behnel stefan_ml at behnel.de
Fri Nov 7 08:18:35 CET 2014


1989lzhh schrieb am 07.11.2014 um 05:48:
>> 在 Nov 7, 2014,02:56,Robert Bradshaw 写道:
>> Here's some proposed function pointer syntaxes; which are the most
>> obvious to understand/read/remember? Can you figure them out?
>>
>>    cdef float (*F)(float)
>>    cdef float (*G)(float (*)(float), float, float)
>>    cdef float ((*H)(char*))(float (*)(float), float, float)
>>
>> vs
>>
>>    cdef float -> float F
>>    cdef (float -> float, float, float) -> float G
>>    cdef (char*) -> (float -> float, float, float) -> float H
>>
>> vs
>>
>>    cdef lambda float: float F
>>    cdef lambda (lambda float: float), float, float: float G
>>    cdef lambda (char*): lambda: (lambda float: float), float, float: float H
>>
>>
>> If you want a hint, the last is something that returns numerical
>> integration algorithm given a string name. Yes, you could use
>> typedefs, but you shouldn't have to. especially for the first.
>>
>    Here are numba kind function annotation, I guess it may fit in here.
> cdef float(float) F
> cdef float(float(float), float, float) G
> cdef float(float(float), float, float)(char*) H
> I personally feel this kind of annotation is more packed that using ->. 

I find it clearer than any of the above. And it still allows the use of
named arguments, i.e. you could say

    cdef char*(float x, int y) F = ...
    F(y=1, x=2.0)

Drawback is that you have to parse up to the name in order to distinguish
it from

    cdef char*(float x, int y):
        ...

Then there's also

    cdef char*(float x, int y) nogil F

And we have to disallow

    cdef char*(float x, int y) with gil F

It gets a bit less readable when you take a proper lower-case variable
name, e.g.

    cdef char*(float x, int y) nogil get_gil

We could fix some of that by allowing

    cdef nogil:
        cdef char*(float x, int y) get_gil

But then, that's adding yet another special case.

Stefan



More information about the cython-devel mailing list