Why can't I give a function's return type the scope storage class?
Meta
jared771 at gmail.com
Sat Mar 16 05:29:08 UTC 2019
Am I wrong in thinking that this is something one would want to
do? It seems like it would be useful for the callee to enforce
that its return value is assigned to a scope variable.
Currently, it seems like there is some sort of inference of scope
on local variables:
struct Test
{
int n = 3;
@safe
int* test() return
{
return &n;
}
}
int* gn;
void main() @safe
{
Test t;
int* n = t.test();
gn = n; //Error: scope variable n assigned to gn with longer
lifetime
}
Which is fine, but I'd like to be able to explicitly say, as part
of a function's contract, that its result may not be escaped from
the caller's scope, and not have to rely on the compiler's
inference. The following doesn't work, however, or doesn't work
as I'd like it to:
@safe
scope int* test() return //Compiles, but doesn't work
{
return &n;
}
@safe
scope(int*) test() return //Does not compile; syntax error
{
return &n;
}
More information about the Digitalmars-d
mailing list