4 ms·
The Thompson story is very telling about C and how semantically powerful it is. KT tried to rewrite UNIX in B at some time, but he failed. Assembly allows you t
by antirez 3y ago
The Thompson story is very telling about C and how semantically powerful it is. KT tried to rewrite UNIX in B at some time, but he failed. Assembly allows you to do many interesting things because it is a low level language, and yet you can build abstractions, so it was a better language than B to write UNIX. But once B got structures, and was starting to become C, things changed. Structures with fields and pointers to other structures is the ha-ha moment of C. It is what really makes the whole language so special. With this simple building block you can start building abstractions that are able to abstract away low level details, while, at the same time, you have full control on low level details.
- rollcat 3y agoDo you see any features in modern, high-level programming languages (such as Python, Go, C#) that could guide the next evolutionary step for C, without sacrificing that low-level control? Knowing KT worked on Go, I'd guess he'd say concurrency, but that's difficult to get right without sacrificing low-level control. What's your take?
- antirez 3y agoSure, C could be much better right now. In some incredible way people that are able to drive the future of C made all the worst choices, undermining the language instead of improving it, for most parts (with undefined behaviours). C just needs: 1. A solid and modern standard library, with basic data structures, sane strings, higher level sockets, ... 2. A very simple object system. No inheritance, no advanced features at all, just the ability to call object->method(1,2,3) and getting a 'self' reference. 3. Ability to perform operations on function exit in order to clean-up locally allocated things. So as I allocate things or open files and alike, I can specify some cleanup code doing things like if (fp) fclose(fp); that is executed when the function returns in any case.
- rini17 3y agoThere are other OOP paradigms, no need to force 'this' pointers on everyone like C++ does. Both 2 and 3 could be done with semantic-aware macros instead.
- antirez 3y agoI believe the C++ way would be the right thing for C. Note that this does not mean in any way to inherit all the stuff from C++, nor even constructors and stuff like that. Simply a way to avoid doing this: list_add(&mylist,1); But instead: mylist->add(1); Note that doing this is not an option: mylist->class->add(&mylist,1); It's not just ugly (even if often used), but also uses an additional pointer, which in some application (like Redis) is a no-go because of memory usage.
- shiomiru 3y agoMaybe you would like UFCS: https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax e.g. Nim has it, and it lets you do this: proc add(self: MyList, e: int) = # [insert append code here] mylist.add(1) # equivalent to add(mylist, 1) Though I'm not sure how well it would work with C, considering it lacks function overloading.
- Arelius 3y agoPerhaps, though I think one should be careful, as reproducing C++ in this fashion runs awfully close to reproducing what I see as C++'s largest sin... Let me explain, I feel iteration speed is of utmost importance. So C++'s mistake of mixing interface and implementation (data layout specifically) leads to massive include bloat, and ultimately contributes significantly to the compilation time problem in C++. In contrast, in C, functions are almost entirely decoupled from structure data layout, and implementation, this promoting much more svelte headers and reduced compilation time for many projects. So, IMO, it would be important to seperate object interface, from the layout, or risk a major, if not the most major, advantage of using C over C++
- 59nadir 3y agoJust as an aside Odin (https://odin-lang.org https://odin-lang.org) has all of these things and does not stray far from C otherwise. It's a delightful language.
- trealira 3y agoIf C hadn't been standardized by ISO, I wonder if C implementors would have eventually added stuff like this just because they found it useful. Or if C had been standardized, but C++ didn't exist, maybe they would have added features like this simply because contemporary languages like Object Pascal and Ada 95 supported object oriented programming better in practice.
- Gibbon1 3y agoI read a paper 'What If We Don’t Pop the Stack? The Return of 2nd-Class Values' where they discuss their research into delaying popping the stack to allow functions to return data allocated on the stack. As a small brained embedded primate that doesn't use malloc that sort of thing would make C vastly easier to use. https://www.cs.purdue.edu/homes/rompf/papers/xhebraj-ecoop22.pdf https://www.cs.purdue.edu/homes/rompf/papers/xhebraj-ecoop22... Also would be super if C had first class types.
- kaba0 3y ago> Structures with fields and pointers to other structures is the ha-ha moment of C. It is what really makes the whole language so special How is it special at all? > you have full control on low level details. Yeah, if you add random attributes to the language and control the compiler. C still can’t properly do vector instructions, while even Java can, but so can C#, Rust, C++. It’s simply not a low-level language.
- flohofwoe 3y agoDid you check out the clang vector extensions? And before "but that's not standard C!": in the C world, the interesting stuff happens outside the standard in language extensions, the standard is mostly irrelevant for real world code. What counts is what specific compilers have to offer.
- antirez 3y agoTo the point that Linux, probably the most important C project in the world, was written in the GCC dialect for many many years. I think it's no longer the case.
- flohofwoe 3y agoAFAIK the only reason why Linux compiles on Clang now too is because Clang tries to emulate GCC behaviour as closely as possible? I doubt that the Linux kernel would compile out of the box with Microsoft's C compiler for instance ;)