Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
D (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Function parameter lifetime annotations within @safe code==== When applied to function parameter which are either of pointer type or references, the keywords ''return'' and ''scope'' constrain the lifetime and use of that parameter. The language standard dictates the following behaviour:<ref>{{Cite web|url=https://dlang.org/spec/function.html#param-storage|title=D Language Specification: Functions - Function Parameter Storage Classes}}</ref> {| class="wikitable" |+ !Storage Class !Behaviour (and constraints to) of a parameter with the storage class |- |''scope'' |References in the parameter cannot be escaped. Ignored for parameters with no references |- |''return'' |Parameter may be returned or copied to the first parameter, but otherwise does not escape from the function. Such copies are required not to outlive the argument(s) they were derived from. Ignored for parameters with no references |} An annotated example is given below.<syntaxhighlight lang="D"> @safe: int* gp; void thorin(scope int*); void gloin(int*); int* balin(return scope int* p, scope int* q, int* r) { gp = p; // Error, p escapes to global variable gp. gp = q; // Error, q escapes to global variable gp. gp = r; // OK. thorin(p); // OK, p does not escape thorin(). thorin(q); // OK. thorin(r); // OK. gloin(p); // Error, p escapes gloin(). gloin(q); // Error, q escapes gloin(). gloin(r); // OK that r escapes gloin(). return p; // OK. return q; // Error, cannot return 'scope' q. return r; // OK. } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)