Jinus017•

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)

171
drakens_jordgubbar•

Need compile time type safety? Just write correct code!

69
n3f4sWRITE 'FORTRAN is not dead'•

Write time type safety: just where correct code

13
Major_BarnulfTiny little god in a tiny little world•

why make a type for that when void* is right there ?

8
aikiigofmt urself•

ah, proof that Rust is just for people who can't write proper tests

3
functorerZygohistomorphic prepromorphism•

My man throwing around the word 'trivial' like an author of a mathematical textbook

149
pareidolistConsidered Harmful•

The implementation is left as an exercise to the reader

82
Battalist•

finding a therapist is too left as an exercise to the reader

25
Sherlockian12•

My man after getting into category theory: "It is actually trivially trivial"

34
functorerZygohistomorphic prepromorphism•

Category theorists are too busy to get people to care about category theory to argue for its triviality

18
lazyubertoadWRITE 'FORTRAN is not dead'•

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!

18
kopkaas2000•

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.

5
lispyfriend•

My favorite is the term "by inspection" meaning, just look at it idiot.

6
the211a•

in every sufficiently large C program there is a bug-ridden, informally-specified, and verbose implementation of half of Java

93
ComfortablyBalancedloves Java•

How can something be more verbose than Java?

20
ineffective_toposZygohistomorphic prepromorphism•

Just what do you not understand about:

((int (*)(void *, char *))((PrintStream *)SYSTEM.out)->class->vtable[PRINT_SLOT])(SYSTEM.out, "Hello World!")

52
TheZipCreatornot Turing complete•

and java is just the adjectives in that sentence

5
Teln0•

Looking at you, libraries that try to add OOP to C and every project that uses them

1
vytah•

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]"

61
capitalsigma•

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

64
james_picaccidentally quadratic•

I refuse to downgrade to 32 bit. I'm sticking with my 16 bit OS.

41
Pjb3005Tiny little god in a tiny little world•

Alright DOS is a little too old and silly, but have you heard about this thing called Itanium?

12
capitalsigma•

No. We got both kinds of ISAs around here: x86 and x86_64

20
maybeJenniferLopezabsolutely obsessed with cerroctness and performance•

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.

4
F54280Considered Harmful•

Yeah, but it is trivially undefined, which is the whole point.

12
[deleted]•

Just trivially use pointers to function pointers.

5
etaionshrd•

Not on POSIX systems

3
lowspeccorgiWhat part of ∀f ∃g (f (x,y) = (g x) y) did you not understand?•

I love making my own vtables

49
grapesmoker•

toothpicks and bubblegum, my favorite design pattern

35
nukasev•

Jesus Christ how primitive. You should really consider using duct tape in your toolkit like the rest of us do.

8
grapesmoker•

I like to keep things pure, we call that referential transparency in the biz

9
wzddWhat’s a compiler? Is it like a transpiler?•

Awww, he even included a fixed-length buffer for the vtable and then advocated for manually-written accessor functions. <3

26
james_picaccidentally quadratic•

This guy doesn't struct.

9
peerlessblue•

"Nothing in your example looks or reads as trivial. That is a gross perversion of a simple procedural language." lmfaoooo

17
tomwhoiscontrarysafety talibans•

typedef struct object object;

But that's JavaScript.

12
suflaj•

incredibly based

11
RidderHaddocklisp does it better•

Seems derived.

13
[deleted]•

[deleted]

1