4 ms·
D (also in Das BetterC) has: 1. underscores in literals: int a = 1_234_567; 2. case ranges: case 5 .. case 6: 3. named arguments: void test(in
by WalterBright 2y ago
D (also in Das BetterC) has:
1. underscores in literals:
int a = 1_234_567;
2. case ranges:
case 5 .. case 6:
3. named arguments:
void test(int a, int b);
void foo() { test(b:3, a:4); }
4. nested functions:
int foo(int i) {
int plus(int a) { return a + i; }
return plus(3);
}
5. static nested functions:
int foo(int i) {
static int plus(int a) { return a + i; }
return plus(3);
}
Error: `static` function `test.foo.plus` cannot access variable `i` in frame of function `test.foo`
6. a feature similar to generators https://dlang.org/spec/statement.html#foreach_over_struct_and_classes https://dlang.org/spec/statement.html#foreach_over_struct_an...
- nialv7 2y agoI was thinking about D the whole way while reading this. I just know I am going to see Walter Bright in the comments XD.
- hermanhermitage 2y agoEvery time Walter posts it reminds me my dream language would simply be C with https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html and probably go lang style interfaces. Maybe a little less UB and some extensions for memory safety proofs.
- WalterBright 2y agoThat's why DasBetterC has done very well! You could call it C with array bounds checking. I occasionally look at statistics on the sources of bugs and security problems in released software. Array bounds overflows far and away are the top cause. Why aren't people just sick of array overflows? In the latest C and C++ versions, all kinds of new features are trumpeted, but again no progress on array overflows. I can confidently say that in the 2 decades of D in production use, the incidence of array overflows has dropped to essentially zero. (To trigger a runtime array overflow, you have to write @system code and throw a compiler switch.) The solution for C I proposed is backwards compatible, and does not make existing code slower. It would be the greatest feature added to C, singularly worth more than all the other stuff in C23.
- actionfromafar 2y agoI don’t always agree but I’ll join you on this particular hill!
- bakul 2y ago> The solution for C I proposed is backwards compatible, and does not make existing code slower. Where can I read about it? The only way to make ptrs to array elements also safe that I can think of, is to replace them with triples: (base, element ptr, limit).
- WalterBright 2y agohttps://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
- bakul 2y agoThanks. I got interested in this topic as people are talking about writing OS kernel code in Rust but a) it only helps new code and b) very hard to justify rewriting millions of lines of C code in Rust (plus rewrites are never 100% faithful feature wise). If on the other hand if C can be made safer, may be through a stepwise process where the code is rewritten incrementally to pass through C->C0->C1->Cn compilers, each making incremental language changes, much more of code can be made safer. It will never be as good as Rust but I do think this space is worth exploring.
- uecker 2y agoI would much prefer a safe C to Rust.
- pjmlp 2y agoI don't even understand how the flip from having C++ collection frameworks being bounds checked by default (Turbo Vision, BIDS, OWL, MFC, Powerplant,...) ended happing, with C++98 getting a standard library that does exactly the opposite by default, and a strong cultural resistance on WG21 to change it until goverments started talking about security liabilities and what programming languages to accept in public projects. As for WG14, I have no hope, they ignored several proposals, and seem keen in having C being as safe as hand writing Assembly code, and even then, Assembly tends to be safer, as UB only happens when doing something the CPU did not expect, macro assemblers don't do clever optimizations.
- akira2501 2y agoWhen writing software I almost never find myself in a situation where UB is a design concern or needs to be factored around in the structure. I almost always find myself struggling to name and namespace things correctly for long term durability. Almost all compiled languages get this wrong. They generally force you to consider this before you start writing code so you can explore the shape of the solution first. I think lisp is the only language I've used where this wasn't a burden, but in reality, lisp then forces you to deeply ponder your data structures and access ideology first, so I didn't find it to be that rewarding in the long run. I love that Go lets you bang simple "method like functions" straight onto the type. This solves the first layer of namespace problems. It does nothing for the second though, and in fact makes it worse, by applying "style guidelines" to the names of the containing types. I am constantly let down by this when writing Go code and I find it hard to write "good looking" code in the language which is all the more frustrating because this was what the guidelines were supposed to solve in the first place. I really just want C and I want to namespace my functions that act on structs into the structs themselves. Then I can name stuff however I want and I don't have to prefix_every_single_function() just so the me and the assembler can fully agree on the unmangled symbol table name which I will almost certainly never care about in 99% of what I compile. There's a real joy to the fast initial development and easy refactoring you can find in scripting languages. Too bad they all have wacky C interfaces and are slower than molasses.
- jefftime 2y agoIf you haven't already I'd check out Zig. It does what you're describing if I am understanding correctly. There are some choices in that language I find annoying, but maybe you'll still enjoy it
- emmanueloga_ 2y agoPerhaps C3? It has interfaces, slices, array iterators and bills itself as "an evolution of C (not revolution) with full ABI compatibility". -- 1: https://c3-lang.org/generic-programming/anyinterfaces/ https://c3-lang.org/generic-programming/anyinterfaces/
- amy-petrik-214 2y agoI was thinking D but also there is basically a superset of D and HighC that incorporates best of both worlds, some people here may know it, it's called "Holy C" and it was the basis for a new operating system in the same way C was tightly integrated with UNIX. Written by Saint Terry
- deleted 2y ago[deleted]
- Gibbon1 2y agoQuestions? You got any idea why people hate the concept of nested functions in C? Why named named arguments test( a:4, b:3) instead of test(.a=4, b.=3); Have any idea how you would handle first class types in C.
- WalterBright 2y ago> You got any idea why people hate the concept of nested functions in C? No, I was surprised to hear it. > Why named named arguments test( a:4, b:3) instead of test(.a=4, b.=3); Consistency with how static initializers work in D.
- kloop 2y ago> You got any idea why people hate the concept of nested functions in C? It breaks the model of c mapping to the hardware. Functions in C are just labels you jump to. Assuming you want to capture values from the environment you're defining the function in, you have to implement some sort of function struct that is callable, and it probably won't work with any shared libraries on your system. Also it would be slightly slower, and C programmers tend to not be willing to make that tradeoff
- WalterBright 2y agoD (and Pascal) implement it by adding and additional parameter to the function called the "static link". (The stack pointer is the "dynamic link".) The static link points to the stack frame of the function it is lexically nested inside. The compiler accesses the variables by offsets to the static link.
- pkphilip 2y ago> Why named named arguments test( a:4, b:3) instead of test(.a=4, b.=3); Why do you consider the second one better than the first (a:4, b:3)?
- lsllc 2y agoIt (the 2nd one) mirrors C's designated initializers for structures: struct foo f = { .bar = 1, .baz = 2, };
- slaymaker1907 2y agoI also think that the GC of D is really a nice feature. Sometimes you want manual memory management in low level code, but there is also a ton of stuff where it doesn't really matter and a GC just makes things easier. For example, if you're writing an in-memory cache service, you really don't want the cache entries themselves to be tracked by the GC since the GC is often unaware of the actual access patterns and just gets in the way, but most of the other components of that service are better served with a GC.
- WalterBright 2y agoOn unexpected nice feature of the GC is it makes compile time function execution easy, as the engine for it doesn't have to emulate your custom storage allocator.