| |
| Posted by vit in reply to mesni | PermalinkReply |
|
vit
| On Sunday, 1 May 2022 at 21:09:14 UTC, mesni wrote:
> It doesn't throw any error
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:
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:(");
}
}
|