3 ms·
In D, the example looks like (if I understood it correctly): struct X { Y y; ref Y getY() return { return y; } } This tells the compiler that getY
by WalterBright 10y ago
In D, the example looks like (if I understood it correctly):
struct X {
Y y;
ref Y getY() return { return y; }
}
This tells the compiler that getY() returns a referenced based on the implicit 'this' reference. It's available now (DIP25 was the proposal for it).
We're now implementing DIP1000, which adds similar support for pointer values (DIP25 only dealt with references).
To see it in action:
alias Y = int;
struct X {
Y y;
ref Y getY() return { return y; }
}
ref Y foo()
{
X x;
return x.getY();
}
Compiling it yields:
test.d(11): Error: escaping reference to local variable x
- tatterdemalion 10y agoAll due respect, but I would be extremely surprised to learn that this feature is as capable as Rust's lifetime system. Whiel you've shown that you can't have an escaping reference to a local variable, what Rust provides goes far beyond that. You can return the references to Y (for example if your function has a reference to X as an argument), you can store them in the heap, you can reassign them to be references to different Ys, etc, all so long as the Ys they point to live longer than they do.
- WalterBright 10y ago> I would be extremely surprised to learn that this feature is as capable as Rust's lifetime system. So would I. It isn't. But we're working on much more than this in order to provide guaranteed memory safety (although using a very different approach than Rust's). It's just that Rust does not stand alone with this particular example.
- tatterdemalion 10y agoI think a key fact about this example is that `&Y` is just another type of value which can be used like any other; if you can't return it our put it in a vector or whatever its really not comparable.
- roca 10y agoThat looks interesting. Looks a lot like the C++ Standard Guidelines lifetime proposal. But it doesn't seem to consider invalidation of references via mutation. For example, could you write a growable array implementation that returns references to its elements, but ensure that those references are not allowed to persist across operations that grow (and hence reallocate) the array? Rust can do this, but I don't think you'll be able to unless you introduce a distinction between mutable and immutable references and integrate it into your safety analysis.
- hasenj 10y agoI think you don't understand the Rust feature the post is talking about. It's not about returning a pointer to a struct. It's about the pointer being _safe_. What if x was not allocated on the stack, but on the heap? y is part of x (not another independent object) so if x is freed/deleted then all references to y become invalid. Does D have a guarantee that this won't happen? (I think the answer is no). This was noted in the post itself: > Most other languages simply prevent you from giving away > an interior reference, or require y to refer to a distinct > heap object from the X.
- Manishearth 10y agoThe D example is also safe. The difference is that the D example doesn't generalize (yet), whereas the Rust example does.
- Manishearth 10y agoWhile this is pretty cool, it would be hard to make it generalize without adding lifetimes or something (feel free to lift that concept off Rust!) While the post gives the example as a simple struct field thing, that example itself isn't where it actually becomes important. It becomes important when you have structs holding pointers to the interiors of other structs, and more complex reference-returning semantics. I illustrate this in a particularly complex situation in http://manishearth.github.io/blog/2015/05/03/where-rust-really-shines/ http://manishearth.github.io/blog/2015/05/03/where-rust-real... But yeah, D is getting there :)