summaryrefslogtreecommitdiff
path: root/docs/examples/userguide/fusedtypes/type_checking.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/userguide/fusedtypes/type_checking.pyx')
-rw-r--r--docs/examples/userguide/fusedtypes/type_checking.pyx28
1 files changed, 28 insertions, 0 deletions
diff --git a/docs/examples/userguide/fusedtypes/type_checking.pyx b/docs/examples/userguide/fusedtypes/type_checking.pyx
new file mode 100644
index 000000000..7bd359739
--- /dev/null
+++ b/docs/examples/userguide/fusedtypes/type_checking.pyx
@@ -0,0 +1,28 @@
+cimport cython
+
+ctypedef fused bunch_of_types:
+ bytes
+ int
+ float
+
+ctypedef fused string_t:
+ cython.p_char
+ bytes
+ unicode
+
+cdef cython.integral myfunc(cython.integral i, bunch_of_types s):
+ # Only one of these branches will be compiled for each specialization!
+ if cython.integral is int:
+ print('i is int')
+ elif cython.integral is long:
+ print('i is long')
+ else:
+ print('i is short')
+
+ if bunch_of_types in string_t:
+ print("s is a string!")
+ return i * 2
+
+myfunc(<int> 5, b'm') # will print "i is an int" and "s is a string"
+myfunc(<long> 5, 3) # will print "i is a long"
+myfunc(<short> 5, 3) # will print "i is a short"