Commit 3461eb95 authored by Stefan Behnel's avatar Stefan Behnel

document specialisation behaviour when a fused memory-view type occurs...

document specialisation behaviour when a fused memory-view type occurs multiple times in a function signature
parent 2e1f162c
...@@ -90,22 +90,23 @@ specialization of the fused type must be the same:: ...@@ -90,22 +90,23 @@ specialization of the fused type must be the same::
return cython.typeof(arg1) == cython.typeof(arg2) return cython.typeof(arg1) == cython.typeof(arg2)
In this case, the type of both parameters is either an int, or a double In this case, the type of both parameters is either an int, or a double
(according to the previous examples). However, because these arguments are the (according to the previous examples). However, because these arguments use the
same fused type of ``my_fused_type``, both ``arg1`` and ``arg2`` must be same fused type ``my_fused_type``, both ``arg1`` and ``arg2`` are
specialized to the same type. Therefore this function returns True for every specialized to the same type. Therefore this function returns True for every
possible valid invocation. You are allowed to mix fused types however:: possible valid invocation. You are allowed to mix fused types however::
def func(A x, B y): def func(A x, B y):
... ...
where ``A`` and ``B`` are different fused types. This will result in specialized where ``A`` and ``B`` are different fused types. This will result in
code paths for all combinations of types contained in ``A`` and ``B``. specialized code paths for all combinations of types contained in ``A``
and ``B``.
Fused types and arrays Fused types and arrays
---------------------- ----------------------
Note that specializations of only numeric types may not be very useful, as one Note that specializations of only numeric types may not be very useful, as one
can usually rely on promotion of types. This is not true for arrays, pointers can usually rely on promotion of types. This is not true for arrays, pointers
and typed views of memory however. Indeed, one may write:: and typed views of memory however. Indeed, one may write::
def myfunc(A[:, :] x): def myfunc(A[:, :] x):
...@@ -116,6 +117,45 @@ and typed views of memory however. Indeed, one may write:: ...@@ -116,6 +117,45 @@ and typed views of memory however. Indeed, one may write::
cdef otherfunc(A *x): cdef otherfunc(A *x):
... ...
Note that in Cython 0.20.x and earlier, the compiler generated the full cross
product of all type combinations when a fused type was used by more than one
memory view in a type signature, e.g.
::
def myfunc(A[:] a, A[:] b):
# a and b had independent item types in Cython 0.20.x and earlier.
...
This was unexpected for most users, unlikely to be desired, and also inconsistent
with other structured type declarations like C arrays of fused types, which were
considered the same type. It was thus changed in Cython 0.21 to use the same
type for all memory views of a fused type. In order to get the original
behaviour, it suffices to declare the same fused type under different names, and
then use these in the declarations::
ctypedef fused A:
int
long
ctypedef fused B:
int
long
def myfunc(A[:] a, B[:] b):
# a and b are independent types here and may have different item types
...
To get only identical types also in older Cython versions (pre-0.21), a ``ctypedef``
can be used::
ctypedef A[:] A_1d
def myfunc(A_1d a, A_1d b):
# a and b have identical item types here, also in older Cython versions
...
Selecting Specializations Selecting Specializations
========================= =========================
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment