summaryrefslogtreecommitdiff
path: root/docs/examples/userguide/fusedtypes/type_checking.pyx
blob: 7bd359739d9275cc0d77ce0b9d6a2cc448ce7c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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"