Fused Types (Templates)

Fused types allow you to have one type definition that can refer to multipletypes. This allows you to write a single static-typed cython algorithm that canoperate on values of multiple types. Thus fused types allow genericprogramming and are akin to templates in C++ or generics in languages likeJava / C#.

Note

Fused types are not currently supported as attributes of extensiontypes. Only variables and function/method arguments can be declaredwith fused types.

Quickstart

  1. from __future__ import print_function
  2.  
  3. ctypedef fused char_or_float:
  4. char
  5. float
  6.  
  7.  
  8. cpdef char_or_float plus_one(char_or_float var):
  9. return var + 1
  10.  
  11.  
  12. def show_me():
  13. cdef:
  14. char a = 127
  15. float b = 127
  16. print('char', plus_one(a))
  17. print('float', plus_one(b))

This gives:

  1. >>> show_me()
  2. char -128
  3. float 128.0

plus_one(a) “specializes” the fused type char_or_float as a char,whereas plus_one(b) specializes char_or_float as a float.

Declaring Fused Types

Fused types may be declared as follows:

  1. cimport cython
  2.  
  3. ctypedef fused my_fused_type:
  4. cython.int
  5. cython.double

This declares a new type called myfused_type which can be _either anint or a double. Alternatively, the declaration may be written as:

  1. my_fused_type = cython.fused_type(cython.int, cython.float)

Only names may be used for the constituent types, but they may be any(non-fused) type, including a typedef. i.e. one may write:

  1. ctypedef double my_double
  2. my_fused_type = cython.fused_type(cython.int, my_double)

Using Fused Types

Fused types can be used to declare parameters of functions or methods:

  1. cdef cfunc(my_fused_type arg):
  2. return arg + 1

If the you use the same fused type more than once in an argument list, then eachspecialization of the fused type must be the same:

  1. cdef cfunc(my_fused_type arg1, my_fused_type arg2):
  2. return cython.typeof(arg1) == cython.typeof(arg2)

In this case, the type of both parameters is either an int, or a double(according to the previous examples). However, because these arguments use thesame fused type my_fused_type, both arg1 and arg2 arespecialized to the same type. Therefore this function returns True for everypossible valid invocation. You are allowed to mix fused types however:

  1. def func(A x, B y):
  2. ...

where A and B are different fused types. This will result inspecialized code paths for all combinations of types contained in Aand B.

Fused types and arrays

Note that specializations of only numeric types may not be very useful, as onecan usually rely on promotion of types. This is not true for arrays, pointersand typed views of memory however. Indeed, one may write:

  1. def myfunc(A[:, :] x):
  2. ...
  3.  
  4. # and
  5.  
  6. cdef otherfunc(A *x):
  7. ...

Note that in Cython 0.20.x and earlier, the compiler generated the full crossproduct of all type combinations when a fused type was used by more than onememory view in a type signature, e.g.

  1. def myfunc(A[:] a, A[:] b):
  2. # a and b had independent item types in Cython 0.20.x and earlier.
  3. ...

This was unexpected for most users, unlikely to be desired, and also inconsistentwith other structured type declarations like C arrays of fused types, which wereconsidered the same type. It was thus changed in Cython 0.21 to use the sametype for all memory views of a fused type. In order to get the originalbehaviour, it suffices to declare the same fused type under different names, andthen use these in the declarations:

  1. ctypedef fused A:
  2. int
  3. long
  4.  
  5. ctypedef fused B:
  6. int
  7. long
  8.  
  9. def myfunc(A[:] a, B[:] b):
  10. # a and b are independent types here and may have different item types
  11. ...

To get only identical types also in older Cython versions (pre-0.21), a ctypedefcan be used:

  1. ctypedef A[:] A_1d
  2.  
  3. def myfunc(A_1d a, A_1d b):
  4. # a and b have identical item types here, also in older Cython versions
  5. ...

Selecting Specializations

You can select a specialization (an instance of the function with specific orspecialized (i.e., non-fused) argument types) in two ways: either by indexing orby calling.

Indexing

You can index functions with types to get certain specializations, i.e.:

  1. cfunc[cython.p_double](p1, p2)
  2.  
  3. # From Cython space
  4. func[float, double](myfloat, mydouble)
  5.  
  6. # From Python space
  7. func[cython.float, cython.double](myfloat, mydouble)

If a fused type is used as a base type, this will mean that the base type is thefused type, so the base type is what needs to be specialized:

  1. cdef myfunc(A *x):
  2. ...
  3.  
  4. # Specialize using int, not int *
  5. myfunc[int](myint)

Calling

A fused function can also be called with arguments, where the dispatch isfigured out automatically:

  1. cfunc(p1, p2)
  2. func(myfloat, mydouble)

For a cdef or cpdef function called from Cython this means that thespecialization is figured out at compile time. For def functions thearguments are typechecked at runtime, and a best-effort approach is performed tofigure out which specialization is needed. This means that this may result in aruntime TypeError if no specialization was found. A cpdef function istreated the same way as a def function if the type of the function isunknown (e.g. if it is external and there is no cimport for it).

The automatic dispatching rules are typically as follows, in order ofpreference:

  • try to find an exact match
  • choose the biggest corresponding numerical type (biggest float, biggestcomplex, biggest int)

Built-in Fused Types

There are some built-in fused types available for convenience, these are:

  1. cython.integral # short, int, long
  2. cython.floating # float, double
  3. cython.numeric # short, int, long, float, double, float complex, double complex

Casting Fused Functions

Fused cdef and cpdef functions may be cast or assigned to C function pointers as follows:

  1. cdef myfunc(cython.floating, cython.integral):
  2. ...
  3.  
  4. # assign directly
  5. cdef object (*funcp)(float, int)
  6. funcp = myfunc
  7. funcp(f, i)
  8.  
  9. # alternatively, cast it
  10. (<object (*)(float, int)> myfunc)(f, i)
  11.  
  12. # This is also valid
  13. funcp = myfunc[float, int]
  14. funcp(f, i)

Type Checking Specializations

Decisions can be made based on the specializations of the fused parameters.False conditions are pruned to avoid invalid code. One may check with is,is not and == and != to see if a fused type is equal to a certainother non-fused type (to check the specialization), or use in and not into figure out whether a specialization is part of another set of types(specified as a fused type). In example:

  1. ctypedef fused bunch_of_types:
  2. ...
  3.  
  4. ctypedef fused string_t:
  5. cython.p_char
  6. bytes
  7. unicode
  8.  
  9. cdef cython.integral myfunc(cython.integral i, bunch_of_types s):
  10. cdef int *int_pointer
  11. cdef long *long_pointer
  12.  
  13. # Only one of these branches will be compiled for each specialization!
  14. if cython.integral is int:
  15. int_pointer = &i
  16. else:
  17. long_pointer = &i
  18.  
  19. if bunch_of_types in string_t:
  20. print("s is a string!")

Conditional GIL Acquiring / Releasing

Acquiring and releasing the GIL can be controlled by a conditionwhich is known at compile time (see Conditional Acquiring / Releasing the GIL).

This is most useful when combined with fused types.A fused type function may have to handle both cython native types(e.g. cython.int or cython.double) and python types (e.g. object or bytes).Conditional Acquiring / Releasing the GIL provides a method for runningthe same piece of code either with the GIL released (for cython native types)and with the GIL held (for python types).:

  1. cimport cython
  2.  
  3. ctypedef fused double_or_object:
  4. cython.double
  5. object
  6.  
  7. def increment(double_or_object x):
  8. with nogil(double_or_object is cython.double):
  9. # Same code handles both cython.double (GIL is released)
  10. # and python object (GIL is not released).
  11. x = x + 1
  12. return x

signatures

Finally, function objects from def or cpdef functions have an attributesignatures, which maps the signature strings to the actual specializedfunctions. This may be useful for inspection. Listed signature strings may alsobe used as indices to the fused function, but the index format may change betweenCython versions:

  1. specialized_function = fused_function["MyExtensionClass|int|float"]

It would usually be preferred to index like this, however:

  1. specialized_function = fused_function[MyExtensionClass, int, float]

Although the latter will select the biggest types for int and float fromPython space, as they are not type identifiers but builtin types there. Passingcython.int and cython.float would resolve that, however.

For memoryview indexing from python space we can do the following:

  1. ctypedef fused my_fused_type:
  2. int[:, ::1]
  3. float[:, ::1]
  4.  
  5. def func(my_fused_type array):
  6. ...
  7.  
  8. my_fused_type[cython.int[:, ::1]](myarray)

The same goes for when using e.g. cython.numeric[:, :].