5 ms·
RAII requires exceptions to be correct. I hesitate to say it it has RAII otherwise. Exceptions have two issues: 1. D exceptions currently require the GC. Ther
by WalterBright 9y ago
RAII requires exceptions to be correct. I hesitate to say it it has RAII otherwise.
Exceptions have two issues:
1. D exceptions currently require the GC. There is a Pull Request to fix that, but it isn't incorporated yet.
2. More problematic is the Dwarf exception handling mechanism requires a language specific "personality" handler. This is supplied by the D runtime library. Trying to trick the C runtime library one into working with D isn't a solved problem at the moment.
- andrewflnr 9y ago> RAII requires exceptions to be correct. I'm not exactly sure what you mean here, but all I really want is guaranteed destructors. Can we just have those?
- WalterBright 9y ago> Can we just have those? It's a good thought. D as Better C is new for us, and we'll be flexible in making it work better.
- millstone 9y agoWhy does RAII require exceptions? RAII is routinely used in C++ with -fno-exceptions.
- WalterBright 9y agoBecause D code may sit in between code that throws and exception and code that handles it. Without EH support, the RAII destructor won't get called when the stack is unwound.
- millstone 9y agoGotcha. C++ has the same problem when its exceptions are thrown through C or -fno-exceptions C++ frames. If that's the only hold-up, IMO the way to go is to abort at the boundary, when an exception would propagate into frames without unwind tables. RAII is too valuable to do without!
- dbaupp 9y agoIsn't it generally undefined behaviour to have exceptions pass through code that isn't compiled with support for them? I.e. not running some destructors is the least of your worries if an exception hits C/-fno-exceptions/-betterC code.
- WalterBright 9y agoUndefined behavior or not, implementations of languages in gcc and Windows tend to support it, even if the language itself does not. I consider it my job to make this work if at all practical, because users do not like ugly surprises. I don't know how Rust's RAII deals with exceptions thrown by lower level code. If someone better acquainted with Rust could chime in here, it would be interesting.
- fasquoika 9y agoIf you mean lower level Rust code, there are no exceptions. Errors are just regular values encoded in the type system. If you're familiar with OCaml optionals and results, it's the same thing
- dbaupp 9y agoThere's a subtlety here: panics are exceptions, but they can be disabled without also disabling RAII.
- steveklabnik 9y ago> I don't know how Rust's RAII deals with exceptions thrown by lower level code. Unwinding across an FFI boundary is considered undefined behavior. https://doc.rust-lang.org/reference/behavior-considered-undefined.html#behavior-considered-undefined https://doc.rust-lang.org/reference/behavior-considered-unde... last bullet point.
- WalterBright 9y agoThat's exactly what I wanted to know. Thanks! Full D is compatible with foreign exceptions and unwinding. I'll see about making D as Better C also compatible, but I suppose Rust's precedent makes it acceptable to not handle it.
- vvanders 9y agoRust uses RAII all over the place and doesn't have exceptions :).