Editions Ideas
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Sun Dec 14 12:57:59 UTC 2025
On 15/12/2025 1:28 AM, Paul Backus wrote:
> On Sunday, 14 December 2025 at 06:49:14 UTC, Walter Bright wrote:
>> On 12/13/2025 9:45 AM, jmh530 wrote:
>>> All else equal, I would recommend trying to more closely follow what
>>> Rust has done, particularly with respect to lifetimes, since that has
>>> traction in the marketplace. I also have found the arguments for an
>>> isolated type to be strong.
>>
>> Lifetimes are what the dip1000 annotations do.
>
> Yes, but Rust's lifetimes are more flexible than dip1000's lifetimes,
> and allow code to be written in a more intuitive way.
>
> For example, it is impossible with dip1000 to declare two variables with
> exactly the same lifetime as each other, even if they are contain the
> same value. To work around this, you have to combine the two variables
> into a single value, like a tuple or a static array--which makes the
> code less natural to read.
To demonstrate that:
```d
scope int* a;
{
scope int* b = new int;
scope int* c = new int;
b = c; // Error: scope variable `c` assigned to `b` with longer
lifetime
a = c; // Error: scope variable `c` assigned to `a` with longer
lifetime
}
```
b and c should have the same lifetime to scope when it comes to
assignments like this. There is no taking a pointer to anything.
However we don't need Rust's approach to annotating it, which functions
as a constraint.
This can be done using scope/return and @escape.
I.e.
```rust
// `<'a: 'b, 'b>` reads as lifetime `'a` is at least as long as `'b`.
// Here, we take in an `&'a i32` and return a `&'b i32` as a result of
coercion.
fn choose_first<'a: 'b, 'b>(first: &'a i32, _: &'b i32) -> &'b i32 {
first
}
```
becomes
```d
ref int choose_first(@escape(return&) ref int* first, ref int* _) {
return a;
}
```
or simplified (with default ref + ref = &):
```d
ref int choose_first(return ref int* first, ref int* _) {
return a;
}
```
More information about the Digitalmars-d
mailing list