Skip to content

Commit 9f63844

Browse files
authored
[spec/function] Improve in parameter docs (#4228)
Mention const before preview switch. Remove unnecessary COMMA use. Add link to scope params. Add example. Use list for defined pass-by-reference behaviour.
1 parent 81929a1 commit 9f63844

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

spec/function.dd

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,20 +1533,37 @@ $(H3 $(LNAME2 param-storage, Parameter Storage Classes))
15331533

15341534
$(H3 $(LNAME2 in-params, In Parameters))
15351535

1536-
$(B Note: The following requires the $(D -preview=in) switch$(COMMA) available in
1537-
$(LINK2 $(ROOT_DIR)changelog/2.094.0.html#preview-in, v2.094.0) or higher.
1538-
When not in use, `in` is equivalent to `const`.)
15391536
$(P The parameter is an input to the function. Input parameters behave as if they have
1540-
the $(D const scope) storage classes. Input parameters may also be passed by reference by the compiler.)
1541-
$(P Unlike $(D ref) parameters$(COMMA) $(D in) parameters can bind to both lvalues and rvalues
1537+
the $(D const) storage class.)
1538+
1539+
$(NOTE The following requires the $(D -preview=in) switch and a compiler compliant with
1540+
$(LINK2 $(ROOT_DIR)changelog/2.094.0.html#preview-in, dmd v2.094.0) or higher.)
1541+
1542+
$(P `in` parameters also behave like $(RELATIVE_LINK2 scope-parameters, `scope`) parameters.)
1543+
1544+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
1545+
---
1546+
const(int[]) g(in int[] a) @safe
1547+
{
1548+
a[0]++; // Error: cannot modify const expression
1549+
return a; // Error: scope parameter `a` may not be returned
1550+
}
1551+
---
1552+
)
1553+
1554+
$(P Input parameters may also be passed by reference by the compiler.
1555+
Unlike $(D ref) parameters, $(D in) parameters can bind to both lvalues and rvalues
15421556
(such as literals).)
1543-
$(P Types that would trigger a side effect if passed by value (such as types with copy constructor$(COMMA)
1544-
postblit$(COMMA) or destructor)$(COMMA) and types which cannot be copied
1545-
(e.g. if their copy constructor is marked as $(D @disable)) will always be passed by reference.
1546-
Dynamic arrays$(COMMA) classes$(COMMA) associative arrays$(COMMA) function pointers$(COMMA) and delegates
1547-
will always be passed by value.)
1548-
$(IMPLEMENTATION_DEFINED If the type of the parameter does not fall in one of those categories$(COMMA)
1549-
whether or not it is passed by reference is implementation defined$(COMMA) and the backend is free
1557+
1558+
- Types that would trigger a side effect if passed by value (such as types with
1559+
a copy constructor, postblit, or destructor) will always be passed by reference.
1560+
- Types which cannot be copied (e.g. if their copy constructor is marked as
1561+
$(D @disable)) will always be passed by reference.
1562+
- Dynamic arrays, classes, associative arrays, function pointers, and delegates
1563+
will always be passed by value.
1564+
1565+
$(IMPLEMENTATION_DEFINED If the type of the parameter does not fall in one of those categories,
1566+
whether or not it is passed by reference is implementation defined, and the backend is free
15501567
to choose the method that will best fit the ABI of the platform.
15511568
)
15521569

0 commit comments

Comments
 (0)