www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - why is the 'return' attribute not working?

reply mesni <mensikovk817 gmail.com> writes:
It doesn't throw any error
```d
import std.stdio;

void main()
{
     int id = IdWrap().getID;
     writeln("use: ", id);
}

struct IdWrap
{
     private int _id;

     int getID() return
     {
     	return _id;
     }

     ~this(){
     	writeln("delete id:(");
     }
}
```

Moreover, if you return `this.someSlice.ptr` in the return method 
in a structure, there will also be no error.
May 01 2022
next sibling parent Dennis <dkorpel gmail.com> writes:
You're not returning a pointer and you're not returning by `ref`, 
so `return` is ignored.

On Sunday, 1 May 2022 at 21:09:14 UTC, mesni wrote:
 Moreover, if you return `this.someSlice.ptr` in the return 
 method in a structure, there will also be no error.
What error do you expect?
May 01 2022
prev sibling parent vit <vit vit.vit> writes:
On Sunday, 1 May 2022 at 21:09:14 UTC, mesni wrote:
 It doesn't throw any error
 ```d
 import std.stdio;

 void main()
 {
     int id = IdWrap().getID;
     writeln("use: ", id);
 }

 struct IdWrap
 {
     private int _id;

     int getID() return
     {
     	return _id;
     }

     ~this(){
     	writeln("delete id:(");
     }
 }
 ```

 Moreover, if you return `this.someSlice.ptr` in the return 
 method in a structure, there will also be no error.
`return` attribute has effect only if return type of function has indirection. For basic types like int has no effect. Example: ```d import std.stdio; void main() safe { int* id = IdWrap().getID; //Error: address of variable `__slIdWrap40` assigned to `id` with longer lifetime writeln("use: ", id); } struct IdWrap { private int _id; property int* getID() safe return { return &_id; } ~this() safe{ writeln("delete id:("); } } ```
May 01 2022