3 ms·
Nothing in your example looks or reads as trivial. That is a gross perversion of a simple procedural language.
by wheelerof4te 4y ago
Nothing in your example looks or reads as trivial. That is a gross perversion of a simple procedural language.
- vidarh 4y agoTrivial to a C developer then. This has been typical of C code for literally decades. The only thing that sucks there is writing wrappers, but it's easy to generate them. From a user point of view you end up doing things like: string * str = string_new(); int len = string_length(str); So it looks just like most other typical C code using prefixes per interface and passing pointers to structs around. In practice, like in C++, you'd tend to only actually use the vtable approach for those things which you actually need to be able to override based on type, which tends to be rare. Another approach is to use messages instead, and a call a generic message handler. E.g. "ob->call(MSG_TYPE, ... arguments)" and just have a pointer to a message handler function as the first pointer in your objects. It's more flexible, but slower.
- wheelerof4te 4y ago"int string_length(void * self) { return ((int ()(void ))((object *)self)->class->vtable[STRING_LENGTH_SLOT])(self);" Oh.My.God. At this point, you might as well be using Java.
- rramadass 4y agoYou are just being glib here. If you want simpler models see my other comments in this thread.
- vidarh 4y agoIf you're writing C, you'll be writing much worse than that on a regular basis. There's a reason I rarely use C any more. But of course if you're going to reimplement OO from scratch in C you will see the entire machinery laid bare. You're only doing it in the first place either because you're committed to using C for whatever reason, or to understand OO implementation strategies.
- vidarh 4y agoReplying to myself to leave this here, since it's vaguely related. If anyone really want to have nightmares about OO'ish concepts in C, here is a few different ways to implement closures in C, going from relatively straightforward, to (unportable) runtime generation of assembler thunks: https://hokstad.com/how-to-implement-closures https://hokstad.com/how-to-implement-closures
- deleted 4y ago[deleted]
- Shorel 4y agoOr C++ /gasp