digitalmars.D.learn - dip1000 return scope dmd v 2.100
Hello, new dmd (2.100) has return/scope changes. It look like examples at page https://dlang.org/spec/function.html#ref-return-scope-parameters are no longer relevant. What difference are between `return scope`, `scope return` and `return`? Why `void* ptr` in struct change effect of `scope return` ? ```d safe: struct A{ int val; //void* ptr; int* test() scope return{ return &this.val; // OK } } struct B{ int val; void* ptr; int* test() return{ return &this.val; // OK } } struct C{ int val; void* ptr; int* test() scope return{ return &this.val; // Error: returning `&this.val` escapes a reference to parameter `this` } } void main(){} ```
May 06 2022
On Friday, 6 May 2022 at 09:24:06 UTC, vit wrote:It look like examples at page https://dlang.org/spec/function.html#ref-return-scope-parameters are no longer relevant.They were recently updated to match the implementation in 2.100.What difference are between `return scope`, `scope return` and `return`?`return scope` means pointer members (such `this.ptr`, `C.ptr`) may not escape the function, unless they are returned. If you call `test()` on a `scope` variable, the return value will be a scope pointer. `scope return` on a struct member is `scope` + `return ref`, meaning pointer members may not escape the function (the `scope` part), but you can return a reference to the struct member itself (`&this.val`, the `return ref` part). If you call `test()` on a local variable (`scope` or not), the return value will be a scope pointer. Just `return` allows you to return a reference to the struct member itself (`&this.val`), and also to escape pointer members (`this.ptr`) since there is no `scope`. However, that means you can't call `test` on `scope` variables.``` int* test() scope return{ return &this.val; // Error: returning `&this.val` escapes a reference to parameter `this` } } ```I think you're using an older DMD version, the error should be gone in 2.100Why void* ptr in struct change effect of scope return ?`scope` is ignored when the struct has no pointers, and before 2.100, the meaning of `return` + `scope` on `ref` parameters was very inconsistent.
May 06 2022
On Friday, 6 May 2022 at 17:17:01 UTC, Dennis wrote:On Friday, 6 May 2022 at 09:24:06 UTC, vit wrote:Thanks[...]They were recently updated to match the implementation in 2.100.[...]`return scope` means pointer members (such `this.ptr`, `C.ptr`) may not escape the function, unless they are returned. If you call `test()` on a `scope` variable, the return value will be a scope pointer. [...]
May 07 2022