How to use heterogeneous list in foreign languages?

I am using ctypes to call the dll functions I created with C3 programming language. C3 is a C like language with manual memory management. C3 offers an any* data type. Which can be thought of as a typed void*. The any* type is recommended for writing code that is polymorphic at runtime where macros are not appropriate. So, how do I use python’s heterogeneous list with this any* type ?

You wrote the API so you know what data types you need to pass into your DLL.
As such I’m not sure what you are asking for.

Maybe an example could help?

1 Like

@barry-scott
Assume that I have a class like this.

makeCombo = c3dll.makeCombo
makeCombo.argtypes = [...]
makeCombo.restypes = ctypes.c_void_p

class ComboBox(Control):
    def __init__(self, parent, x, y, w, h, items, ...):
        self._items = items
        self._ptr = makeCombo(..., self._items, ....)

This is the sample code. I get a python list from the parameter items and it is heterogeneous. How do I pass it to C3 world ?
The user can use it like this.

cmbItems = ["Africa", 1998, "Neptune", 15.2, True, "Sample"]
cmb = ComboBox(parent, 10, 10, 100, 30, cmbItems, ...)

I have no idea it is your API! The void * points to what exactly in your API?

Is it, for example a struct with 6 fields? If so make that struct using ctypes and pass it in.

1 Like

You will need to figure out how any* maps to C (not C3) data types, so you should look into the implementation details of the C3 language. I would imagine it is some kind of tagged union. You might be able to do something like provide “constructors” in the dll so that you can call cmbItems = [c3dll.str2any("Africa"), c3dll.int2any(1998), ...]

2 Likes

@MegaIng
Thank you for the reply. The author of C3 is really helpful and he is available at discord. So I can solve that side of the problem. I think constructors with python’s isinstance feature would solve this. Let me try.