Commit f8d10420 authored by Stefan Behnel's avatar Stefan Behnel

Rewrite the section on fused types in function signatures to make it clearer...

Rewrite the section on fused types in function signatures to make it clearer how multiple fused types interact here.
parent 1ce05b86
...@@ -63,24 +63,40 @@ Fused types can be used to declare parameters of functions or methods:: ...@@ -63,24 +63,40 @@ Fused types can be used to declare parameters of functions or methods::
cdef cfunc(my_fused_type arg): cdef cfunc(my_fused_type arg):
return arg + 1 return arg + 1
If the you use the same fused type more than once in an argument list, then each If the same fused type appears more than once in the function arguments,
specialization of the fused type must be the same:: then they will all have the same specialised type::
cdef cfunc(my_fused_type arg1, my_fused_type arg2): cdef cfunc(my_fused_type arg1, my_fused_type arg2):
return cython.typeof(arg1) == cython.typeof(arg2) # arg1 and arg2 always have the same type here
return arg1 + arg2
In this case, the type of both parameters is either an int, or a double Here, the type of both parameters is either an int, or a double
(according to the previous examples). However, because these arguments use the (according to the previous examples), because they use the same fused type
same fused type ``my_fused_type``, both ``arg1`` and ``arg2`` are name ``my_fused_type``. Mixing different fused types (or differently named
specialized to the same type. Therefore this function returns True for every fused types) in the arguments will specialise them independently::
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 This will result in specialized code paths for all combinations of types
specialized code paths for all combinations of types contained in ``A`` contained in ``A`` and ``B``, e.g.::
and ``B``.
ctypedef fused my_fused_type:
cython.int
cython.double
ctypedef fused my_fused_type2:
cython.int
cython.double
cdef func(my_fused_type a, my_fused_type2 b):
# a and b may have the same or different types here
print("SAME!" if my_fused_type is my_fused_type2 else "NOT SAME!)
return a + b
Note that a simple typedef to rename the fused type does not currently work here.
See Github issue :issue:`4302`.
Fused types and arrays Fused types and arrays
---------------------- ----------------------
......
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