Tell us your DIP1000 woes
Paul Backus
snarwin at gmail.com
Tue Aug 27 14:56:36 UTC 2024
On Sunday, 25 August 2024 at 13:10:22 UTC, Mike Parker wrote:
> If you've had problems using DIP1000, please post here with a
> description of what you encountered and any details about it
> you think we may find helpful. Please provide example code
> where possible.
Here's an issue I ran into when working on my proof-of-concept
allocator/container library.
When writing exception-safe code, a common pattern is to save the
original value of a variable before performing an operation that
might throw, and then restore that value if the operation fails.
Unfortunately, it is impossible to do this in `@safe` code if the
variable you want to save and restore is `scope`, because the
compiler will treat the saved copy as having a shorter lifetime
than the original.
For example, this code fails to compile when using
`-preview=dip1000`:
@safe void example(ref scope int[] var)
{
int[] savedValue = var;
scope (failure) var = savedValue;
// Do stuff with a that might throw...
}
...with the following error message:
onlineapp.d(4): Error: scope variable `savedValue` assigned
to `ref` variable `var` with longer lifetime
Ideally, the compiler should be able to infer that `savedValue`
has the *exact same* lifetime as `var` in this code, and allow
the assignment.
More information about the Digitalmars-d
mailing list