'Cython how to create an anonymous union inside of a struct?

I want to create the following structure in cython (actually I don't really need this one in but it is a simplified example.)

cdef struct MyStruct:
    int x
    union:
        int y
        long z

I can fix it by doing the following.

cdef union MyUnion:
    int y
    long z

cdef struct MyStruct:
    int x
    MyUnion u

However the issue is that if I want to access y or z I would need to say struct_var.u.y rather than struct_var.y. Is there a way to create anonymous unions inside of a struct?



Solution 1:[1]

If you're interfacing with an external library, you can simply flatten the structure. Cython just needs the names and types for lexical lookup.

cdef struct MyStruct:
    int x
    int y
    long z

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1