[Cython] [sage-devel] Re: New function (pointer) syntax.

Robert Bradshaw robertwb at math.washington.edu
Fri Nov 7 06:04:28 CET 2014


Thanks for all the feedback!


On Thu, Nov 6, 2014 at 7:13 PM, Travis Scrimshaw <tscrim at ucdavis.edu> wrote:
> Here's my 2 cents.
>>
>>
>>     cdef float (*F)(float)
>>     cdef float (*G)(float (*)(float), float, float)
>>     cdef float ((*H)(char*))(float (*)(float), float, float)
>
>
> I prefer this one because it mimics C function pointers (at least how I
> remember, I'm somewhat rusty).

It is C function pointer syntax... if you consider that a plus (aside
from compatibility).

> When I saw the "->", I thought of pointer
> redirection (without reading the rest of the post which mentioned it) and
> believe it could be a major source of confusion.

I'm surprised so many people are noting this; good feedback.

> However since cython is
> suppose to mimic python as much as possible (please correct me if I'm wrong
> here), perhaps the lambda one is better?

Yeah, it's a bit odd, but less confusing.

A goal is a syntax such that when you look at it you know what it
means, unlike, say, the C syntax that you have to learn how to decode.
When I first saw it in Miranda ages ago, but clearly others think of C
first when they think of something of type "float -> float."



On Thu, Nov 6, 2014 at 8:06 PM, Peter Schay <pschay at gmail.com> wrote:
>
> The C function pointer syntax is clever and makes a lot of sense but
> reading/writing a complicated declaration usually feels like I'm solving a
> puzzle.

Exactly what I want to avoid. Certainly violates several points of the
Zen of Python.

> IANACG (I am not a compiler guy) so I am sure there must be some flaw in the
> following but I can't help asking whether there would be anything wrong with
> this form, which reads more like a sentence ("a pointer to a function F with
> args x which returns y"):
>
> cdef lambda F(float): float
> cdef lambda G(
>   (lambda (float): float),
>   float,
>   float
> ): float
> cdef lambda H(char *): (lambda (float, float, float): float)

In Python "lambda (a, b): ..." with the parentheses means a function
taking one argument that's unpacked into two variables.

> One could also use an underscore for the unnamed lambdas, maybe.

One of the main goals is to move away from C style declarators, i.e.
actually have "cdef [type] [name]" instead of "cdef [basetype]
[mix_of_names_and_type_info]. So one would write

cdef (float -> float) f, g, h

to declare three variables of type float -> float. Then we might have
hope of an actual, comprehensible grammar for Cython. (Few folks think
in terms of declarators, so one would write "int[10] x" to declare an
array of size 10 rather than "int x[10].")



On Thu, Nov 6, 2014 at 8:46 PM, Nathann Cohen <nathann.cohen at gmail.com> wrote:
> Hello !
>
> To me the second looks the most clear. Except that I would have written
>
> cdef (char*) -> ((float -> float, float, float) -> float H) instead of
>
> cdef (char*) -> (float -> float, float, float) -> float H
>
> But well, one gets used to things like that.

Yeah, cdef ( (char*) -> ((float -> float, float, float) -> float) ) H
would be just fine as well for those who like parentheses.

> It is clearer but indeed is it surprising to find such a syntax in Cython...
> Actually like Travis I would expect Cython to support the first, even though
> it is the least obvious...

It does now.

> If both are implemented I will definitele use the second whenever I can :-)

Me too.




On Thu, Nov 6, 2014 at 8:48 PM, 1989lzhh <1989lzhh at gmail.com> wrote:
>
>    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 ->.

That's an interesting alternative. Would float(float)* be a pointer to
a function pointer then? (float*)(float) something that returned a
pointer to a float?

- Robert


More information about the cython-devel mailing list