Struct with overloaded methods or ability to print cppclass

17 views
Skip to first unread message

Source

unread,
Feb 4, 2023, 6:13:01 PM2/4/23
to cython-users
Hey, I noticed although apparently you should be able to overload struct methods in C++, Cython does not seem to support this, only for cppclass types.

And when defined as a cppclass I cannot print the thing (cannot convert vector[object] to a Python object), which is highly convenient for me to be able to do.
I also cannot pass the object anymore to Python functions such as asyncio loop.call_later.
This is detrimental.

Is there a solution?

D Woods

unread,
Feb 5, 2023, 4:09:54 AM2/5/23
to cython-users
So Cython takes a "C" interpretation of structs (i.e. they can't have methods) and a C++ interepretation of classes. I don't think we want to change that.

In real C++ structs and methods are identical except for default visibility - there's no good reason to use one over the other. So there isn't a C++ reason to support structs with methods.

> And when defined as a cppclass I cannot print the thing (cannot convert vector[object] to a Python object), which is highly convenient for me to be able to do.
> I also cannot pass the object anymore to Python functions such as asyncio loop.call_later.
> This is detrimental.

Presumably this refers to the auto-conversion between structs and dicts. Personally I'm keen not to add more auto-conversions - the existing ones cause real confusion, and also mislead people into thinking that they're just "passing the object" where actually they're making a full and expensive copy.

You're probably better off making a cdef class wrapper of your struct/class and passing that around.

Source

unread,
Feb 5, 2023, 11:43:39 AM2/5/23
to cython-users
I see.

Well, it needs to be a C/C++ struct/class to be memory efficient. These objects are also stored in a C++ map, which if I recall correctly cdef classes cannot be stored in because they can contain Python objects (and probably other technical reasons that's beyond my specific knowledge). I'm basically writing a large C++ program with Python support, not the other way around, if that makes sense.
In fact my dream is to port at least 80% of the codebase to C++ and just "import" it with an extern from, but currently that's not feasible.

Real unfortunate if they cause confusion and that is I guess understandably discouraging you guys from adding more such features, as they're part of a set of very powerful features of the language that is what makes it so powerful in my opinion, from debugging to writing switch-cases in a single line, sad to hear it's not going to continue in that direction. I'm not currently sure where that leaves me. I guess I just have to stick with structs with methods, which I believe is supported, but without being able to overload them, or keep my current setup with just a ton of singleton functions for everything (or they're technically part of a cppclass, but it's a whole mess due to existing bugs with not being able to overload non-extern cppclasses either, etc) which is not pretty or very orderly, but no other choice as it is right now.

Source

unread,
Feb 10, 2023, 1:45:13 AM2/10/23
to cython-users
To be clear about a couple of points:
1) I did another test just to confirm and C structs can indeed have methods in Cython, just not overloaded methods. And
2) I don't need auto-conversion, but manual/explicit conversion would be nice, which is currently not supported. If auto-conversions were confusing people, you could remove all auto-conversions and replace them with explicit conversions, or mention that auto-conversions are costly in the documentation so be wary of it? Any confusion from that point forwards seems like an issue with the developer, not with the language/tool.

And to be clear although not mentioned it's also not the print function that is crucial, but loop.call_later(...) and str(vector[object]).encode() and so on which can be directly sent to the client through the network. But for now I guess I'll just stick with structs with methods that cannot be overloaded since the conversion is crucial for me, adding more wrappers is just not a solution, the program already has a 60 seconds compilation time and is only half done and remembering all the function names is already becoming troubling (10k lines of code remember) so simple conversions, like I've gotten used to rely on, are necessary. If they were removed altogether one day I would rely on an older version of Cython much rather than create a million more wrappers (for every loop.call_later(...), for every conversion in different forms - for example not just str(vector[object]).encode() but if I need to format it differently using list or dict comprehensions, etc).

Overall you know I love your language, I've said this before, it's the most powerful language I'm personally aware of.
But there are still some confusing features or lack of features/bugs such as struct methods but no overloading, dropped overloading of non-extern cppclasses, conversion of structs but not classes, etc, we've talked about these before.
But that's alright, I know fixing bugs takes time and wish you guys the best progress forward, and I really hope the future doesn't look like no conversions period and bugs intentionally left forever because of this comment :(
Best of luck!
Reply all
Reply to author
Forward
0 new messages