[Dlang-internal] DIP1000 discussion and testing
Dicebot via Dlang-internal
dlang-internal at puremagic.com
Sat Oct 22 16:22:50 PDT 2016
Ok, so I am revisiting DIP1000 right now based on recent comments from
Walter. One thing remains unclear to me though, consider this example in
DIP1000 document:
```
@safe struct RefCountedSlice(T) {
private T[] payload;
// ...
// interesting fact #2: references to internals can be given away
scope ref T opIndex(size_t i) {
return payload[i];
}
}
```
It looks like with current semantics this statement is very wrong.
Assuming that you have meant `scope return` instead (because plain scope
does nothing for `this` reference) Right now it does not matter how
`opIndex` is annotated if variable holding `RefCountedSlice` itself is
not annotated/deduced as scope:
```
@safe struct S
{
// RefCountedSlice code reduced to bare minimum
private int* payload;
this (int) {
this.payload = new int;
}
@trusted ~this () {
delete this.payload;
}
int* get () scope return {
return this.payload;
}
}
int* global;
@safe unittest
{
scope s = S(42);
global = s.get(); // Error: scope variable s assigned to global with
longer lifetime
}
@safe unittest
{
auto s = S(42);
global = s.get(); // compiles, oops
}
```
And indeed, with your explanations it seems to work as intended - `scope
return` annotation is simply forwarding lifetime of `S.payload` to
return value, which happens to be infinity by lifetime rules (GC
allocation). But that means that statement saying destructor can be
@trusted is wrong.
Walter, would you mind showing reference counted example that actually
works under your implementation?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/dlang-internal/attachments/20161023/3f757537/attachment.sig>
More information about the Dlang-internal
mailing list