Polymorphism in C is trivial: You use a vtable - a struct of method pointers ... return ((int (*)(void *))((object *)self)->class->vtable[STRING_LENGTH_SLOT])(self);
news.ycombinator.com/item?id=34298135Posted Jan 8, 2023, 12:49 PM UTC
Posted Jan 8, 2023, 12:49 PM UTC
My man throwing around the word 'trivial' like an author of a mathematical textbook
The implementation is left as an exercise to the reader
finding a therapist is too left as an exercise to the reader
My man after getting into category theory: "It is actually trivially trivial"
Category theorists are too busy to get people to care about category theory to argue for its triviality
That's, like, how that is under the hood in many languages! That's the basics! How do you think the polymorphism works in your Java? You must know it!
#define UNJERK
The author just messed and overcomplicated everything. It should be like
int string_length(struct string * self) {
return self->vtable->length(self);
}
The vtable struct declaration will be a bit whacky, just because function pointers declarations in C are like that. Yes, I did implement virtual polymorphism in C once.
#undef UNJERK
C needs to upgrade the macros just a lil bit, so this would be just a macro. Maybe even introduce something like SFINAE for macros!
Anything to avoid just writing
int string_length (struct string *self) {
// Here's where you calculate the length of the fucking string in *self
}
Obviously the superior way is some kind of vtable-scheme, and depending on magic elves to make sure that all objects that implement a length call have it in the same index in the vtable. It's so simple, I'll leave multiple inheritance as an exercise for the reader.
My favorite is the term "by inspection" meaning, just look at it idiot.
in every sufficiently large C program there is a bug-ridden, informally-specified, and verbose implementation of half of Java
How can something be more verbose than Java?
Just what do you not understand about:
((int (*)(void *, char *))((PrintStream *)SYSTEM.out)->class->vtable[PRINT_SLOT])(SYSTEM.out, "Hello World!")
and java is just the adjectives in that sentence
Looking at you, libraries that try to add OOP to C and every project that uses them
This code exhibits undefined behaviour.
void (*vtable[NUM_SLOTS]); defines a table of void pointers, but they are later cast to pointers to functions:int (*)(void *). Void pointers and pointers to functions are incompatible in C. (Void pointers are only compatible with pointers to objects.)
EDIT: an example of an environment where this would break is DOS using the medium memory model: pointers to objects (and therefore also void pointers) are 16-bit, pointers to functions are 32-bit.
EDIT 2: Obligatory "trying to write a line of C code without invoking UB challenge [IMPOSSIBLE]"
I appreciate that you called this out because clearly no serious engineer would fail to account for compatibility with medium memory model DOS in the year of our lord 2023
I refuse to downgrade to 32 bit. I'm sticking with my 16 bit OS.
Alright DOS is a little too old and silly, but have you heard about this thing called Itanium?
No. We got both kinds of ISAs around here: x86 and x86_64
If I ran a CI server that builds and tests Itanium builds of our product, I'd double the number of computers running on Itanium worldwide.
Yeah, but it is trivially undefined, which is the whole point.
Just trivially use pointers to function pointers.
Not on POSIX systems
I love making my own vtables
toothpicks and bubblegum, my favorite design pattern
Jesus Christ how primitive. You should really consider using duct tape in your toolkit like the rest of us do.
I like to keep things pure, we call that referential transparency in the biz
when you tell wagies that oop didn't invent data types
Hi. For background, I have been writing professional OOP code in ANSI C.
Awww, he even included a fixed-length buffer for the vtable and then advocated for manually-written accessor functions. <3
This guy doesn't struct.
"Nothing in your example looks or reads as trivial. That is a gross perversion of a simple procedural language." lmfaoooo
typedef struct object object;
But that's JavaScript.
incredibly based
Seems derived.
[deleted]
All of C is trivial. Need a data structure? Just implement it yourself, nothing complicated. Need memory safety? Just free everything you malloced. Need to avoid undefined behaviour? Just read the specs. Need generics? Just use unions or void pointers. Want first-class functions? Function pointers have you covered. There is no reason for you to use other languages with their fancy useless features, such as methods, inheritance, interfaces, mona
Segmentation fault (core dumped)
Need compile time type safety? Just write correct code!
Write time type safety: just where correct code
why make a type for that when
void*is right there ?ah, proof that Rust is just for people who can't write proper tests