enhancements funcpointers

robertwb edited this page Nov 14, 2014 · 3 revisions
Clone this wiki locally

CEP 528 - Function pointer syntax

  • Status: Under discussion
  • Implementation status: Mostly trivial

Cython uses C-style declarators to declare function pointers, which is counter-intuitive to those not coming from a C background and breaks the cdef [type] [var] pattern. In order to make it easier for cdef functions to be stored and passed around as first-class objects, and to simplify the grammar, it is worth exploring alternative syntaxes that could be used.

Existing C-declarators

cdef float (*f)(float)
cdef float (*integrate)(float (*)(float), float, float)
cdef float ((*get_integrator)(char*))(float (*)(float), float, float)

Pros

Cons

Haskell-style arrows

cdef float -> float f
cdef (float -> float, float, float) -> float integrate
cdef (char*) -> (float -> float, float, float) -> float get_integrator

Pros

  • Similar to many functional languages, math formulae, and even Java8.

Cons

  • -> operator has a different meaning in C.

lambda types

cdef lambda float: float f
cdef lambda (lambda float: float), float, float: float integrate
cdef lambda (char*): lambda: (lambda float: float), float, float: float get_integrator

Pros

  • Easy to interpret with no prior background, unambiguous.

Cons

  • Verbose/clunky to write.

numba-style

cdef float(float) f
cdef float(float(float), float, float) integrate
cdef float(float(float), float, float)(char*) get_integrator

Pros

Cons

mypy-style

Function[[float], float] f
Function[[Function[float, float], float, float], float] integrate
Function[[char*],  Function[[Function[float, float], float, float], float] get_integrator

Pros

Cons

Other??