Re: dip1000 return scope dmd v 2.100

2022-05-07 Thread vit via Digitalmars-d-learn

On Friday, 6 May 2022 at 17:17:01 UTC, Dennis wrote:

On Friday, 6 May 2022 at 09:24:06 UTC, vit wrote:

[...]


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.


[...]


Thanks


Re: dip1000 return scope dmd v 2.100

2022-05-06 Thread Dennis via Digitalmars-d-learn

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 
(``, 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 (``), 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  // Error: returning `` 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.




dip1000 return scope dmd v 2.100

2022-05-06 Thread vit via Digitalmars-d-learn

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// OK
}
}

struct B{
int  val;
void* ptr;

int* test() return{
return  // OK
}
}

struct C{
int  val;
void* ptr;

int* test() scope return{
		return  // Error: returning `` escapes a 
reference to parameter `this`

}
}


void main(){}
```