dip1000 return scope dmd v 2.100

Dennis dkorpel at gmail.com
Fri May 6 17:17:01 UTC 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.100

> Why 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.



More information about the Digitalmars-d-learn mailing list