Edit: I think it was called "Herbception"s after Herb Sutter, and it really sounded like a good idea to me
I used to be a big fan of C++ exceptions, but eventually soured on it for various reasons. Here's an article partially addressing it from a while back:
I would classify D's scope exit/failure/success as RAII actually, even if D uses a GC.
Sometimes you might not need exceptions and something like std::expected or optional is better.
In my case I use expected for some network APIs since I expect failures to happen out of my control aspart of the flow of my program, but I do not see why I would not use exceptions in many other situations, such as for non-ignorsble errors. I could think of a lack of disk space or some other fatal error thst is not under the control of the program.
If you forget to handle this, the error will cascade.
Also, exceptions do not make the signature of a function change (at least not in C++, Java checked exceptions is different). This means that the plasticity for adding errors at any depth of the call stack augments without bypassing any error silently.
All in all, I would say exceptions should be the main mechanism in normal circumstances and for expected errors you csn use error/result types.
I personally much prefer Rust style Result which also can't be ignored, and puts fallibili5y in the function signature.
D's scope exit/failure/success is built on top of RAII and has nothing to do with the GC.
> I would say exceptions should be the main mechanism in normal circumstances and for expected errors you csn use error/result types.
Come to DConf (or watch the live stream) and I hope I can change your mind, or at least challenge your conclusions!
It has better than that, but it is a bit clunky compared with C++(just use struct instead of class). You can have proper destructors, but if you have a container you need to be more careful than in C++.
In D exceptions are the default like in C++ and you have to opt out with nothrow or extern(C) or betterC.
Really try some D code in a bigger situation it is 90% good and almost worth using over C++, but if you can't have GC it is a huge pain, but better than C.
Additionally, Khalil Estell has made an excellent work proving that the way exceptions are currently implemented is not optimal and there is plenty of room for improvement, when someone cares about their implementation.
"Cutting C++ Exception Time by +90%? - Khalil Estell - CppCon 2025"
In the end, it was DOA due to compiler architecture, so that was the end of that beautiful design.
I've had to accept result types is about as good as its going to get, all that is missing to make them nice is unwrapping support in if statements. Which I too have a proposal for and implemented it. But alas politics.
I can never get past the ugly syntax.
But the "I can never get past the ugly syntax." reason is unusually superficial from you. Understanding Lisp macros or CLOS (the very cool object system) or Conditions (which are like exceptions, but also different in a very interesting and powerful way) is totally worth enduring the syntax for a language designer.
If you find the time, give it a try! Either way: I am a big D fan, so I agree with your tastes most of the time. Thanks for your work :)
I find the ((()))) aspect of it difficult to read, whereas the algebraic notation is easy. I've struggled with trying to read Lisp code for decades. It's a proverbial square peg that won't fit in the round hole.
A major aspect of D is the aesthetic look of the code. It's why I call D "elegant" as it just looks good on the screen. For example, this is how modules are imported:
import foo;
Short and sweet. No syntactic noise.Standard exception handling is:
- Whenever an error happens, the program puts a description of it into an “exception” object. You go from the innermost dynamic scope to the outermost looking for handlers. Once you find a handler willing to accept that type of exception, you unwind to the point where it was installed then invoke it, passing the exception object.
Condition handling[3] is:
- Whenever an error happens, the program puts a description of it into a “condition” object. You go from the innermost dynamic scope to the outermost looking for handlers. Once you find a condition handler willing to accept that type of condition, you invoke it as a regular callback, without unwinding, passing the condition object.
- The handler then packs up some data into a “restart” object. You go through all the dynamic scopes again (remember that the erroring function is still active). Once you find a restart handling willing to accept this type of restart, you unwind to the point where it was installed then invoke it, passing the restart object.
(I am omitting things outside the happy path: a way for the exception/condition handler to punt, what happens if the condition handler does not invoke a restart, etc.)
As far as the benefits of this two-phase approach, Practical Common Lisp gives an example[4] of a single-record parser that raises an “invalid record” condition, a loop around it that installs a “skip to next record” restart, and finally the caller can make the policy decision on what to do for invalid records.
As another example, Common Lisp signals an “unbound-variable”[5] condition leaving the “use-value” and “store-value” restarts in scope, then the REPL installs a handler that offers them interactively. (My own half-serious example of a DOS abort/retry/fail prompt is in this vein too, chosen mostly because it feels strange that you can’t do it in a conventional exception system.)
[1] https://package.opendylan.org/dylan-programming-book/excepti...
[2] https://news.ycombinator.com/item?id=31196046
[3] https://www.nhplace.com/kent/Papers/Condition-Handling-2001....
[4] https://gigamonkeys.com/book/beyond-exception-handling-condi...
[5] https://www.lispworks.com/documentation/HyperSpec/Body/e_unb...
By the way, big fan of D even though I don't get to use it much. Appreciate your work!
Edit: Also would be nice to adopt move and copy semantics even closer to C++ and maybe need less explicit moves, and ideally less mess with calling __xdtor when trying to do RAII
there's a contributor working with both upstream and opend adding wasi support as well, and he also got exception support working, we'll probably ship that next month.
so the full d story on wasm is progressing too.
You might say if it is so easy, why not do it upstream? It takes years of political arguments and random ghosting to get anything done up there, all while constant (regression inducing) code churn breaks your PRs every other month, so you spend 3x the time rebasing than you ever spent writing the implementation before it is merged...... if it ever gets merged at all. I have several successful contributions to upstream D, it happens, but most the work done there is ignored. You might get some encouraging forum replies, but when it comes time to ship it? Crickets. Several former D contributors jumped ship for Zig many years ago, and it was a real loss for us (and real gain for them).
With the opend, I know if it works and delivers real world value, I can ship a release, dmd and ldc together, so minimal duplicated waste work.
What should you use? idk, opend is basically my pet project, provided in the hope that it will be useful, but THIS SOFTWARE IS PROVIDED `'AS IS″ AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE so your mileage may vary. Still, I maintain about a half million lines of code, some of which is quite old, so keeping some stability is important to me while moving forward with select common-sense, incremental improvements. Most D code works fine either way though, I've done web programming a long time, in the old school tradition, so words like "progressive enhancement" and "graceful degradation" are meaningful to me, even when being a compiler maintainer.
Oof, yeah that is definitely a pain point. Not to get too personal, but why would you continue to invest so much time in the language if upstream isn't accommodating?
Has WalterBright had any comments on this? He usually is so vocal, any hope of a remerge of the OpenD changes in to CanonD?
Anyway, I appreciate you trying to advance Dlang, I fight for it all the time at work, and usually get to do stuff in it(I am old and have privileges).
You can find such @nogc collections in `dplug:core` or `nulib` packages.
> I would like to see Phobos 3 be more betterC forward.
It is, some part of it is going to be called BaseD and with a more restricted subset, without GC usage.
There's a lot of smaller things that haven't been pulled though, like i merged a bug fix to module naming, which was an upstream PR from like 2018, a fix on library file name generation, a regression from 2019 but has a trivial workaround, a fix to redefined reflection names which everyone finds annoying, the implementation was written in 2017, but they had endless debates about syntax and i just made an executive decision and moved on but they still bikeshed... stuff like that, they're all little things but minor annoyances every time you hit them and the fixes were easy, so just do it!
Then the two bigger things I did they have talked about but probably won't do is i made the class monitor opt-in, this is a relatively big breaking change, I had to do fixes on 8 different projects. Each one took a few minutes, so not a huge deal, but still. They talked about this in an upstream meeting but decided against taking it (for now at least).
And then I changed the default init of all built in types to zero, including char and float, and this is controversial since float init to nan has legitimate advantages, and the breakage can be subtle if you don't catch it. I was on the fence personally, it more like a 51-49 vote rather than an obvious bug fix, but it is easier to explain to newbies that int and float both init to 0. In theory, you are supposed to explicitly initialize all these all the time, but in practice we know it is different.
So I think the competitive edge is pushing them a little. And I'm not anti-collaboration; I write blogs about my implementations with the hope that they'll give a little code review and that's been semi-successful, like the upstream backporters have indeed fixed bugs in my implementations so I'm happy to share back and forth, we both win that way.
FYI that had very little to do with OpenD merging the tuple syntax - at least from my perspective - Timon may feel differently. I was the one who got the ball rolling on the DIP for it and kept pushing the process forward, and I also helped Timon and Nick get it over the finish line by fixing an ICE in the backend caused by destructuring a tuple of structs.
I've wanted first class tuple syntax in D since 2013, and when Timon mentioned not having time to do a DIP for it, I offered to help. It would've happened regardless of whether it got merged into OpenD or not.
but maybe this is the first step toward something more useful.
My experience was the ldc impl was MUCH easier.... and optimizes much better, no surprise, but also compiled WAY slower. On dmd, the null checks barely affected compile speeds at all, but on ldc, the runtime impact isn't bad... but the compile time hit is ENORMOUS. I didn't even notice it at first, because I mostly use ldc just for cross compiles (another thing opend makes easy, `opend install xpack-win64` for example makes Windows builds just work, and it can do icons built in without third party toolchain as well, something hipreme took to upstream via his redub program that copied my implementation. he then added similar for Mac but I havent' pulled that back to opend yet, i will though) and I misdiagnosed.
Went looking for why the compile times were so much worse in opend ldc vs upstream (and btw note my definition of "so much worse" is like 3 seconds instead of 2 seconds, 50% is signifcant) and tried PGO and diffs in codegen and llvm versions...... then realized no, it is the null checks.
So the ldc implementation was easy and seems bug free (the bugs rikki fixed were in obscure corners of dmd's backend), and it optimizes well for runtime..... but wow it takes its sweet time to do that optimization. I'd appreciate someone else taking a look at that some day since there might be some better way to do it than I did (i basically copied the RangeError implementation on pointers for null check).
But I expect some day it'll come and then hopefully we'll make both of our compilers better.
- It has a type system that catches most category errors, without having to think too much about types
- It is quite easy to read and write, and code in it has a high signal to noise ratio
- It provides a GC, but also allows you to write idiomatic code that doesn't allocate a lot (missing from a lot of gc languages, it feels like most GC languages force you to allocate more for every abstraction you write)
- It supports GC style code for things that don't need to be efficient (>98% of the code I actually write)
- I can easily micro-optimize the inner-loop code that does need to be efficient, without having to switch languages or setup ffi. I can also be relatively certain that the GC isn't firing too often in those loops, since the gc only collects when you gc-allocate
- I can use all of the native libraries with C bindings on my system, with practically zero costs for bindings
- Metaprogramming in it is top notch, I feel like every time I needed to do some type level shenanigans, I could do so in a way that actually looked like code in the end, without needing to maintain a code-generator. The way the metaprogramming works also stays fairly readable, it's not like macro_rules! or #defines or templates.
Overall, it has superseded C for pretty much everything I previously used C for, and is a joy to use for a lot of other usecases as well, I've found myself reaching for it for programs that I would otherwise write in python recently. Only major weakness in my daily experience is that you can't compile it to WASM, so I'm still using rust for that. I've considered learning Zig or Odin, but I feel like I would miss the GC for simple tasks, and D is good enough on most axes that I stopped looking for a new one true programming language to write all my code in. int foo() { return bar(); }
int bar() { return 3; }