why is the 'return' attribute not working?
vit
vit at vit.vit
Mon May 2 06:50:07 UTC 2022
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:(");
}
}
```
More information about the Digitalmars-d
mailing list