4 ms·
Just say no, that path leads to madness. C is not the right language for fancy abstractions, it will come back and bite your head off when you least expect it.
by sifoobar 8y ago
Just say no, that path leads to madness. C is not the right language for fancy abstractions, it will come back and bite your head off when you least expect it. It can be done, but that doesn't mean worth doing. GObject suffers from some of the same delusions.
The whole point of writing C is to keep it simple and only build exactly what's needed. No other language I've tried comes close to supporting the same level of custom fitting. This is also the reason it's potentially really fast; more because it encourages simple, direct solutions than anything else.
Virtual dispatch is a function pointer, either embedded directly in the value or together with others in a shared vtable. A type id may be added to the vtable if needed. Avoid inheritance tricks, composition is mostly the right answer in C. Downcasting is a solved [0] problem [1].
[0] https://gitlab.com/sifoo/snigl/blob/master/src/snigl/type.c#L181 https://gitlab.com/sifoo/snigl/blob/master/src/snigl/type.c#...
[1] https://gitlab.com/sifoo/snigl/blob/master/src/snigl/util.h#L17 https://gitlab.com/sifoo/snigl/blob/master/src/snigl/util.h#...
- mpweiher 8y agoNot just that, I also just don't understand the point. " a small C library which implements high-level concepts available in Clos, Objc" Well, Objective-C is "a small C library" which implements the high-level concepts available in Objective-C. Mostly. At some point, the creators found out that they needed a "compiler" to gather up and unique selectors, and at that point adding a small amount of syntactic sugar was easy and useful. An object is a struct with pointer to a class as its firs element. (A class is another struct, it is also an object) And 'id' is a pointer to such an object. A selector (SEL) is a uniqued C-String. A method is a C function that takes an object and a selector as its first arguments: -hi:there { } is roughly the same as id hi( id self, SEL _cmd, id there ) { } (The naming is slightly different) Sending a message is calling the function objc_msgSend(). And if you don't like the syntactic additions, just call objc_msgSend() yourself. Those who don't understand Objective-C are doomed to reinvent it?
- OskarS 8y agoYes, it seemed strange to me as well. "We really, really like Objective-C and decided to poorly implement it in C". Just use Objective-C! It's perfectly compatible with C, it has better syntax and better compilers with better error messages. This is just crazyness.
- potiuper 8y ago[1] is #define[d] as sgl_baseof(type, field, ptr) that appears to used to return a pointer to a "type", which is a value stored in memory at run-time, of a field in an object (ptr)? I and many others would not consider a run-time type system to be "the" solution to downcasting in C. Regarding the use of C for "fancy abstractions" such as classes and objects, the derivatives of Cfront (Corneau C/C++), which is an implementation of C++ using C, are not just alive, but have been chosen to implement many production systems (https://en.wikipedia.org/wiki/Edison_Design_Group https://en.wikipedia.org/wiki/Edison_Design_Group) used by millions today, although they too "inherit" the issues with the C standard. It is true that the GObject system has many problems one of which is the reference counting (sink/sunk) process as requiring a certain memory (de)allocation mechanics as a requirement of the object system is not the most applicable in certain situations.
- DonHopkins 8y ago>requiring a certain memory (de)allocation mechanics as a requirement of the object system is not the most applicable in certain situations To destruct -- to collect. To collect -- perchance to tenure: ay there's the rub! For in that collection of destruction what cycles may come When we have shuffled off this mortal core, Must give us callbacks.
- gpderetta 8y agoYou have it backward, Comeau is a compiler implemented with the EDG front end not the other way around. Also I'm not sure there is any shared history between Comeau and Cfront. Finally, as far as I understand Cfront was a proper C++ compiler that simply targeted C instead of ASM (like many compilers for other languages do ), not some sort of C preprocessor (Cfront was initially bootstrapped with a c-with-classes preprocessor though).
- potiuper 8y agoComeau is a compiler implemented with the EDG front end. If the other way around was some how implied, then it was not the intention. As for Comeau and Cfront, Comeau Computing's CEO, Greg Comeau, provided one of the early ports of cfront to the PC: https://en.wikipedia.org/wiki/Comeau_C/C%2B%2B https://en.wikipedia.org/wiki/Comeau_C/C%2B%2B. Cfront is not just a preprocessor and if the comparison is limited to only C library implementations of objects such as COS, then CPre / C with Classes would be the applicable choice and not Cfront. But, the original point was not meant to exclude "trans"pilers or compilers that output to C such as Genie, Vala, etc.