16 ms·
D as a C Replacement
- jfries 7y agoCool! -betterC looks well worth exploring. Also, pure and safe are fantastic features, I just wish the default was the other way around, so you need to specify impure and unsafe.
- mhh__ 7y agoIf the language were being done today I suspect that could've ended up being the case, but keep in mind that (given that idiomatic D encourages use of templates) these will be inferred automatically upon instantiation of a given template
- SantiagoElf 7y agoTo be honest, I was always fascinated by the D language. I am glad it is still alive and kicking. Evolving too :)
- AstroJetson 7y agoI've preferred C to C++ and D to C++ for a long time. To me when I read C++ code it's like the modem disconnected in the middle of the editing session with all the : and << flying around. For things that don't need classes (and there is a lot of code) I stick to C. But if I need the OOP benifits then I go with D. As a bonus, the C programmers and JavaScript programmers that I work with can read the D code and get most of it on the first try. If you haven't used D, spend 30 mins and write a four function calculator. Then take the remaining time adding some kind of internal function line sin, cos, etc. You'll be suprised at how fast you can pick it up. Granted, the above is a toy exercise, it's not writing an enterprise solution. But it's a little more than the standard "Hello Sailor, new in town" program that people start off with.
- petre 7y ago> For things that don't need classes (and there is a lot of code) I stick to C. I'd use D anyway just for the GC or whatever, less compiler fighting, betterC etc. Procedural, OOP, functional, yay.
- rowanG077 7y agoReally? For me the presence of a GC disqualifies D as the main replacement for C and C++.
- mhh__ 7y agoHow much software genuinely requires not having a GC? If you don't want it, don't use it, but using it can make your software completely safe and fast (Garbage Collection is often faster than RC as long as pause times are acceptable, and added memory use). A lot of the standard library ranges are inherently lazy and therefore require no interaction with the GC. Exceptions are also nogc now.
- quicknir 7y agoIf you can tolerate GC then like 20 languages (as or less obscure than D) enter the conversation. If you can tolerate GC and the performance implications that go with it, then using C or C++ is rarely a good choice to begin with (well, other than legacy, which is a valid and common reason).
- mhh__ 7y agoD isn't defined by performance or memory management, however. There are many D features which are just blisteringly professional in how they approach software development: Using D is conducive to writing good software. Metaprogramming, for example, in D is obvious and almost fun. The compile times are ridiculous compared to C++, i.e. I can build the main D compiler in three different configurations in about 5sec on my machine.
- 7y ago
- nextos 7y agoI also prefer C and D to C++. But used wisely, C++ can be a really nice language after all changes that started with C++11. The key is to realize C++ is not a language, but a federation of languages. You need to cherry pick a subset you what you want to use and stay disciplined. Otherwise, it is a mess.
- msla 7y ago> The key is to realize C++ is not a language, but a federation of languages. You need to cherry pick a subset you what you want to use and stay disciplined. Otherwise, it is a mess. I've noticed two strains in language design, which I internally call Scheme-style and Common Lisp-style: Scheme-style languages are the trimmed-down languages, often built around a big idea which animates the rest of the design but not always; the defining concept of Scheme-style languages is always how much they can remove to get a pure design, a design which supports writing code in one style. Python, Smalltalk, C, and Scheme are all languages in this mold. Java is kind of a compromised Scheme-style language, or two of them (C and Smalltalk) rammed into each other at high speed, which also goes for Objective-C. Common Lisp-style languages have mini-languages inside of them, or at least support multi-paradigm programming as a core design goal; you can trim a Common Lisp-style language into a Scheme-style subset, but you can't extend a Scheme-style language into something more Common Lisp-like unless you really are dealing with Scheme and have (hygienic) macros to use to add more syntax. C++, Perl, and Common Lisp are all Common Lisp-style; Unix shell is Common Lisp-style in practice because it encourages programmers to write in a bunch of relatively Scheme-style languages at once, all in the same program. Note that both Scheme and Common Lisp have a conceptual core in the Lambda Calculus and axiom-like special forms. C++ lacks this and Perl doesn't really have it either, but most people don't write huge codebases in Perl.
- strictfp 7y ago>> I've preferred C to C++ and D to C++ for a long time. To me when I read C++ code it's like the modem disconnected in the middle of the editing session with all the : and << flying around. I'm sorry, but even though I think D is a great language, there are plenty of usecases where D isn't a viable alternative to neither C or C++. If D had all those missing features, or if you compared D to some other mainly-GC:ed, statically typed and compiled language, I think the syntax comparison would have been more fair.
- mhh__ 7y agoWhich missing features?
- dymk 7y agoSay, the whole C++ ecosystem
- 256 7y agoI don't think that's fair. D interfaces with C++ better than any other language I can think of. See https://dlang.org/spec/cpp_interface.html https://dlang.org/spec/cpp_interface.html
- pjmlp 7y agoDelphi and .NET also come to mind, due to C++ Builder and C++/CLI respectively. :) Or via COM, which D also supports.
- nicwilson 7y agoIts getting there https://github.com/atilaneves/dpp https://github.com/atilaneves/dpp
- mhh__ 7y agoEven when I only really need C, the contract programming that D provides is really helpful as it provides a macro free way of doing rigorous assertions etc. in a way that I can turn off without infecting the actual logic code. If a struct's payload cannot be null, for example, getting the compiler to insert checks into all uses in debug builds can be done easily That and metaprogramming in D is all compile time so you get RSI-free hands, for free.
- pjmlp 7y agoC++20 is also getting contracts, just as info. The more languages that support DbC the better.
- vram22 7y agoEiffel may have been one of the first languages to support DbC (Design by Contract). https://en.wikipedia.org/wiki/Design_by_contract#History https://en.wikipedia.org/wiki/Design_by_contract#History
- umvi 7y agoI stopped using `<<` for the most part after I discovered the excellent fmt library[1]. [1] https://github.com/fmtlib/fmt https://github.com/fmtlib/fmt
- Ace17 7y ago> To me when I read C++ code it's like the modem disconnected in the middle of the editing session with all the : and << flying around. Visual appearance is a rather weak reason for preferring one language over another (especially if it's for explicitly personal reasons).
- Crinus 7y agoWeak or not, it still is a reason as visual appearances affects readability.
- sametmax 7y agoVisual appearance and cognitive load are very much linked IMO.
- rramadass 7y agoVery true. This is one of the things i dislike most about templates in C++. Even today reading template code with its nested syntax takes me time to unpack and understand. Symbols do affect comprehension and i would be quite interested in knowing whether somebody has done a study w.r.t. the various programming languages.
- lacampbell 7y agoD is one of those language that while it's technically very good, it's small eco system makes me look over it. I'm quite conservative about what libraries I rely on, and with these smaller languages 90% of the libraries you want are someones hobby. The betterC scenario has intrigued me before though, it seemed like a win all around since you'd be using C libraries anyway. I'd love to hear others experiences and anecdotes with this.
- vaylian 7y ago> it's small eco system makes me look over it. You have relatively clean access to all C libraries. But I do not know if that is relevant enough in your area of work/play. And the standard library is also pretty nice. Sure, it's nowhere near Python's Batteries-Included package, but it can get you quite far.
- 0815test 7y ago> You have relatively clean access to all C libraries. I thought much of the point of D as a programming language would be its "relatively clean" access to external C++ libraries, which is not a feature many other languages share? (whereas it is of course quite common to have a foreign-function interface with C.)
- bachmeier 7y ago> its "relatively clean" access to external C++ libraries They're working on it, but there's a lot more work to do: https://dlang.org/spec/cpp_interface.html https://dlang.org/spec/cpp_interface.html There's a reason other languages don't have easy C++ interoperability. > it is of course quite common to have a foreign-function interface with C Yes, but it's not trivial for the most part to interface with C code. D is designed to make it easy - to the point that you can #include a C header file and you're good to go.
- flukus 7y agoIt looks like you still have to write interfaces for c libraries (https://dlang.org/spec/interfaceToC.html https://dlang.org/spec/interfaceToC.html) and that it can't use header files directly like zig. This is fine for interop but it's not great as a drop in replacement, there is still some manual work and likely many edge cases around macros in the c libraries. I'm yet to put zig through it's paces but you can include header files directly, theoretically that makes it a much better drop in replacement.
- dataking 7y agoScope guards seem like a really good idea. Has there been any effort to write a C to -betterC transpiler just like there has for Rust (e.g. C2Rust [0], Corrode [1])? [0] https://www.github.com/immunant/c2rust https://www.github.com/immunant/c2rust [1] https://www.github.com/jameysharp/corrode https://www.github.com/jameysharp/corrode
- mhh__ 7y agoYes. D's design is based around the idea that semantically valid C will compile in D (apart from crud like function pointer syntax etc.) but there are tools to convert C to D (As used in the dmd compiler backend (which dates back to the 80s I believe, but is now in a tasty form of spaghetti D)
- earenndil 7y ago> D's design is based around the idea that semantically valid C will compile in D AFAIK it's that code which compiles in both c and d should have the same behaviour; not that c code should necessarily compile in d. Examples of c that don't work in d are trivial. > there are tools to convert C to D (As used in the dmd compiler backend (which dates back to the 80s I believe, but is now in a tasty form of spaghetti D) Dmd's backend was ported by hand. What tools are those? I know there are tools to convert header files, but I don't know of any that actually convert code.
- mhh__ 7y agoSemantically valid as in valid D, not valid C. > Dmd's backend was ported by hand I stand corrected then. Visual D has one cpp2d built in, which I assumed was used
- nicwilson 7y ago> Dmd's backend was ported by hand. Not quite, the c++ source was massaged to be free of weird corner cases of the grammar and preprocessor (e.g. enforcing no spaces between '#' and the preprocessor keyword) and then a tool called magicport was used to convert the frontend in one shot. The backend was mostly converted by "hand" (i.e. grep after some more massaging). There are several tools to translate headers though dstep[1] and dpp[2] come to mind [1]: https://github.com/jacob-carlborg/dstep https://github.com/jacob-carlborg/dstep [2]: https://github.com/atilaneves/dpp https://github.com/atilaneves/dpp
- buserror 7y agoI never understood the use of D, or any of the "C replacements" languages trends. C does work pretty well, but it's like a sharp tool. Use with a lot of care with your remaining fingers. There are a LOT of things C is blamed for, and languages try to introduce complicated syntaxes and abstraction to prevent them from happening, however, most of these other languages are ALSO open to the same security issues (or sometime, more), just on a different level. Perhaps they look a lot safer because they haven't had the same level of scrutiny as good old C. In the meantime they introduce a 'duh/WTF' effect when you try to bring someone else in on the codebase. C with good static analyser coverage (cppcheck, clang 'scan-build', smatch[0]) and reasonable discipline is pretty easy to maintain, VERY easy to debug, extraordinarily low footprint and very fast, so taking the time and having a BIT of discipline to harness that power is well worth it. As the article mentions, I'm one of the guys who did 20 years of C++ before 'reverting back' to C, and I'd LOVE a few of the things I've had to give up -- however, a new language is not really something I need. [0]: http://smatch.sourceforge.net http://smatch.sourceforge.net
- AgentME 7y ago>most of these other languages are ALSO open to the same security issues How often do programs in non-C/C++ languages get remote code execution vulnerabilities? I'm sure the CVE numbers will overwhelmingly show that most RCE-capable vulnerabilities are in C/C++. Sure, it occasionally happens in other languages, but practically only when someone does something like misuse one of a specific set of APIs that allow code loading. In C/C++, any code that slightly fucks up writing to an array or passes a pointer to previously-freed memory around can cause a RCE vulnerability that lets an attacker literally run code on your system and take it over. Inspecting a C/C++ codebase made by a junior person for vulnerabilities is a total nightmare. Inspecting a codebase in any other language for RCE vulnerabilities generally means you grep for a couple APIs and look at how they get used.
- buserror 7y agoI don't believe that, you mention this because you put 100% trust on the runtime. When I see syntax like: var array=something[4...$] I don't see security there, all I see is someone pushing the problem further under the carpet, into the runtime. So Sure, RIGHT NOW the runtime might be safe from scrutiny, but is it because its SAFE or just not as such a high profile, just yet? On the other hand I DO agree that 'junior' programming will always be a problem, regardless of the matter, and pretty much, regardless of the language.
- asveikau 7y agoThe linked article on C++ method pointers loses some relevance now that C++11 and newer have lambdas and std::function. Still an interesting rabbit hole. I agree that the Digital Mars implementation looks like the sanest.
- quelsolaar 7y agoI disagree about being better then C. Compile time asserts can be created using #error in C, and having something happen out of order at the end of a scope is a terrible idea. One of the main strengths of C is that everything is explicit and nothing happens without you saying so.
- mhh__ 7y agoThen don't use them? They are completely explicit, you have to write scope(exit) to make anything happen. The use of scope(exit) is actually very useful because, for example( short of using RAII) how many times do we all forget to free or close a file. Programmers make mistakes, but if you give them the tools to not fuck up as idiomatic parts of the language: They will use them.
- Skunkleton 7y ago> One of the main strengths of C is that everything is explicit and nothing happens without you saying so. That's not strictly true. For example, complex object assignment _might_ be turned into a function call by the compiler.
- ben-schaaf 7y ago> Compile time asserts can be created using #error Really not the same thing. Compile time asserts allow you to check any compile time value. For instance say you were writing OpenGL code and your struct needs to have a size that is a multiple of 16 bytes. In D that's easy: static assert(sizeof(Data) % 16 == 0);
- nn3 7y agobuild time asserts can be defined in C macros with some tricks. The error messages are slightly misleading, but when people look at the source code it is usually clear what is happening. #define ASSERT_BUILD(x) \ do { char array[(x) ? 1 : -1]; (void)array; } while(0) static char foo[10]; ASSERT_BUILD(sizeof(foo) >= 10);
- lifthrasiir 7y ago
- alganet 7y agoVery dool. I dompletely agree.
- rubber_duck 7y agoWhats the story with D portability ? Can it compile to iOS Android and WASM without a lot of issues ? C and C++ can compile to anything - thats a major use case for me
- earenndil 7y agoD does technically run on all three of those platforms (ios, android, wasm). Of the three, android is probably the most mature. Wasm is not very usable, because wasm itself doesn't yet have a garbage collector, so the d runtime is not ported. For iOS, there is a fork of GDC (GCC-based compiler) which is supposed to run, but there isn't very much interest in it. If you put in the work, you could likely get all three to work, but there's nothing OOTB yet.
- mhh__ 7y agoD uses the same backends, so in principle it's similar coverage to C++ (gdc targets loads and loads of architectures, for example) however druntime hasn't been ported to many. WASMV is completely doable but there's no C environment( at all) so you have to do it all yourself
- mgregory22 7y agoI thought it was going to be an article about batteries before I clicked
- kmmbvnr_ 7y agoI was shocked by title, b/c I thought it was about vitamins.
- jordigh 7y agoD has been a real pleasure for me. I did the 2017 AoC in it: http://jordi.inversethought.com/blog/advent-of-d/ http://jordi.inversethought.com/blog/advent-of-d/ The richness and flexibility of compile-time features almost make it feel like a lisp, with the slight downgrade of sometimes working on strings of source code instead of sexps. Regardless, like the article says, D's metaprogramming alone is worth the entry price of reading a few books or tutorials about the language.
- MrRadar 7y agoThe way you characterize D's metaprogrammability is exactly how I would put it too. It's hard to describe but D just makes metaprogramming so fun and easy compared to anything besides Lisp and I find it sorely missing when working with most other languages.
- quicknir 7y agoI think the author blows his credibility very early with this comment: > As I said, it’s a superficial example, but I think it shows a general difference in philosophy between C++ and D. (If I wanted to make the difference even clearer, I’d use an example that needed iomanip in C++.) iostreams are not in C++ because C++'s philosophy is actually that iostreams are great. Everyone knows they suck. They are there because without variadics, there isn't a good way to do type safe text output in the style of printf. And I mean, not even runtime type safe. Chaining of some kind is the obvious way to simulate variadics when you don't have variadics. And at the time, they thought it was better to get something type safe into the standard library, then gate it behind variadics which could (did) take a long time. Voila, iostreams. C++ is quite literally in the process of standardizing a library that will bring type safe printf (a la D) to C++. The same way that 8 years ago, C++ finally managed to standardize variadics after quite a lot of effort. The disadvantage of being an old language is that it can be hard to stay caught up with features. The advantage is that you get a huge base of existing developers, knowledge, libraries, etc. Bloggers love to make things about big picture philosophy because it makes for better blurbs but many things in reality are just engineering decisions. D had the luxury of creating metaprogramming syntax from scratch after one if its creators was one of the main people to discover the power of "accidental" TMP in C++. C++ is still trying to bend accidental TMP into something more bearable to use without breaking everything. The differences here are more practical than philosophical.
- munificent 7y ago> They are there because without variadics, there isn't a good way to do type safe text output in the style of printf. I think an equally important point is that IO streams in C++ are extensible. You can make your own user defined types work with them. There's no way to add "printf() support" to your own type in C.
- flukus 7y ago> I think an equally important point is that IO streams in C++ are extensible. Not sure how portable it is but you can define conversions for your own types: http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html http://www.gnu.org/software/libc/manual/html_node/Customizin... . The format and format_arg attributes give you compile time support: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attribute... .
- dnautics 7y agoThe post this is referring to claimed as its major complaints about rust its 1) lack of a spec and 2) its increasing number of features, neither of which are addressed by the author.
- xedrac 7y agoI am admittedly a D novice. I would choose it over C in a heartbeat for most things. However, with how good Rust has become, it's hard for me consider D for anything. Rust is a fantastic C/C++ replacement IMO, and I'm hugely invested in C++.
- atemerev 7y agoWell, if constantly fighting borrow checker is your thing...
- hollerith 7y agoSome people bristle at any restriction on their freedom of action (which psychologists sometimes call "trait reactivity") whereas some people are OK with a restriction if there are practical benefits.
- cinnamon_bun 7y agoI would call this phenomenon "cowboy development". Why would you fight borrow checker when you can employ your users to flush out crashes and undefined behavior?
- pjmlp 7y agoBecause one is foolish to try to implement GUIs in Rust, which most GC languages are much better suited for.
- atemerev 7y agoThere are other languages who achieve the same goal (C-like performance, controlled latency, memory safety) by less intrusive means. E.g. Nim, or D. So yes, I am perhaps one of those “some people”, if there are other ways to safety, I prefer my freedom of action unconstrained.
- anfilt 7y agoD is not a C replacement. It's more targeted at C++. Honestly, I prefer D to C++, but still prefer C to D. I would say the biggest turn off to C is generally it's very small STD library. While there are a lot C libraries that exist to achieve certain things it's not part of STD C. It's not part of the "out of box" experience. It also depends on what your programing if you looking at system level stuff currently the two languages I would look at are C and Rust. Heck even for some system stuff your still required to use assembly. Anything else choices are more flexible and productivity might matter more. Honestly, despite C's flaws very few languages occupy it's niche and flexibility. Like you have to be doing something right to some degree to last almost 50 years. I would say a lot this comes down to C's simplicity yet still having a lot of power as a language.
- xvilka 7y agoZig[1] looks like more interesting and modern alternative, albeit immature one. [1] https://ziglang.org https://ziglang.org
- deleted 7y ago[deleted]
- DeathArrow 7y agoI don't see the need for a C replacement. We have 2 use cases: a) you absolutely need performance, so a GC is out of question b) you don't need absolute performance In case a) I don't think there are better solutions than C In case b) you wouldn't use C, to begin with. C#, Go, Java are ok.
- auvrw 7y agoadditionally, there's (c) kernel and/or embedded. in particular, and i've read that writing kernels isn't feasible in Go. it appears not to be an issue of performance. something about the runtime prevents writing kernels altogether. like the linked article says https://drewdevault.com/2019/03/25/Rust-is-not-a-good-C-replacement.html https://drewdevault.com/2019/03/25/Rust-is-not-a-good-C-repl... rust has a lot of features (generics, etc.) aside and apart from the memory management strategy. i'm also unaware of a procedural language that has the feature stability of C or Go as well as the ability to write kernels or embedded code with the memory safety of rust. currently wondering how easy it'd be to tack rust's memory model onto another language, alef in particular.
- pjmlp 7y agoIn both cases I would use C++. C is only relevant as long as we will have UNIX and POSIX around.
- koff3 7y agoI like D a lot but find it's not a good fit for many things. C++, Java and Python have great tools and libraries to rely on. Not so much with D's small community. Writing wrappers and interop always feels like a hassle, so it's easier to stick with just one language. First time I used D was before the version 2, back when there was the Tango library. D seems to have improved quite a bit since then, but it's still in the same position as before.