Skip to content

Commit 45a07ee

Browse files
authored
Merge pull request #2972 from ntrel/inout
spec/function.dd: Improve inout section
2 parents 157d923 + 0be61df commit 45a07ee

File tree

1 file changed

+58
-30
lines changed

1 file changed

+58
-30
lines changed

spec/function.dd

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ $(GNAME Parameter):
4343
$(I ParameterAttributes)$(OPT) $(GLINK2 type, Type) $(D ...)
4444

4545
$(GNAME ParameterAttributes):
46-
$(I InOut)
46+
$(GLINK InOut)
4747
$(GLINK2 attribute, UserDefinedAttribute)
48-
$(I ParameterAttributes InOut)
48+
$(I ParameterAttributes) $(GLINK InOut)
4949
$(I ParameterAttributes) $(GLINK2 attribute, UserDefinedAttribute)
5050
$(I ParameterAttributes)
5151

@@ -646,52 +646,80 @@ $(H2 $(LNAME2 auto-ref-functions, Auto Ref Functions))
646646

647647
$(H2 $(LNAME2 inout-functions, Inout Functions))
648648

649-
$(P Functions that differ only in whether the parameters are mutable, const or immutable
650-
and have corresponding mutable, const or immutable return types can be combined
651-
into one function with the $(D inout) type constructor.
649+
$(P Functions that differ only in whether the parameters are mutable, `const` or `immutable`,
650+
and have corresponding mutable, `const` or `immutable` return types, can be combined
651+
into one function using the $(D inout) type constructor. Consider the following
652+
overload set:
652653
)
653-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
654654
---
655-
int[] f1(int[] a, int x, int y) { return a[x .. y]; }
655+
int[] slice(int[] a, int x, int y) { return a[x .. y]; }
656656

657-
const(int)[] f2(const(int)[] a, int x, int y) { return a[x .. y]; }
657+
const(int)[] slice(const(int)[] a, int x, int y) { return a[x .. y]; }
658658

659-
immutable(int)[] f3(immutable(int)[] a, int x, int y) { return a[x .. y]; }
659+
immutable(int)[] slice(immutable(int)[] a, int x, int y) { return a[x .. y]; }
660660
---
661-
)
662661

663-
$(P The code generated by these three functions is identical.
664-
To combine into one function, the $(D_KEYWORD inout)
665-
type constructor is employed:)
662+
$(P The code generated by each of these functions is identical.
663+
The $(D_KEYWORD inout) type constructor can combine them into one function:)
666664

667665
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
668666
---
669-
inout(int)[] foo(inout(int)[] a, int x, int y) { return a[x .. y]; }
667+
inout(int)[] slice(inout(int)[] a, int x, int y) { return a[x .. y]; }
670668
---
671669
)
672670

673-
$(P The $(D_KEYWORD inout) forms a wildcard that stands in for
674-
any of mutable, const, immutable, inout, or inout const. When the
675-
function is called, the inout of the return type is changed to whatever
676-
the mutable, const, immutable, inout, or inout const status of the
677-
argument type to the parameter inout was.
671+
$(P The $(D_KEYWORD inout) keyword forms a wildcard that stands in for
672+
mutable, `const`, `immutable`, `inout`, or `inout const`.
673+
When calling the function, the `inout` state of the return type is changed to
674+
match that of the argument type passed to the `inout` parameter.
678675
)
679676

680-
$(P Inout types can be implicitly converted to const or inout const,
681-
but to nothing else. Other types cannot be implicitly converted to inout.
682-
Casting to or from inout is not allowed in @safe functions.
677+
$(P `inout` can also be used as a type constructor inside a function that has a
678+
parameter declared with `inout`. The `inout` state of a type declared with
679+
`inout` is changed to match that of the argument type passed to the `inout`
680+
parameter:
681+
)
682+
683+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
684+
---
685+
inout(int)[] asymmetric(inout(int)[] input_data)
686+
{
687+
inout(int)[] r = input_data;
688+
while (r.length > 1 && r[0] == r[$-1])
689+
r = r[1..$-1];
690+
return r;
691+
}
692+
---
693+
)
694+
695+
$(P Inout types can be implicitly converted to `const` or `inout const`,
696+
but to nothing else. Other types cannot be implicitly converted to `inout`.
697+
Casting to or from `inout` is not allowed in `@safe` functions.
683698
)
684699

685-
$(P A set of arguments to a function with inout parameters is considered
686-
a match if any inout argument types match exactly, or:)
700+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
701+
---
702+
void f(inout int* ptr)
703+
{
704+
const int* p = ptr;
705+
int* q = ptr; // error
706+
immutable int* r = ptr; // error
707+
}
708+
---
709+
)
710+
711+
$(H3 $(LNAME2 matching-an-inout-parameter, Matching an `inout` Parameter))
712+
713+
$(P A set of arguments to a function with `inout` parameters is considered
714+
a match if any `inout` argument types match exactly, or:)
687715

688716
$(OL
689-
$(LI No argument types are composed of inout types.)
690-
$(LI A mutable, const or immutable argument type can be matched against each
691-
corresponding parameter inout type.)
717+
$(LI No argument types are composed of `inout` types.)
718+
$(LI A mutable, `const` or `immutable` argument type can be matched against each
719+
corresponding parameter `inout` type.)
692720
)
693721

694-
$(P If such a match occurs, the inout is considered the common qualifier of
722+
$(P If such a match occurs, `inout` is considered the common qualifier of
695723
the matched qualifiers. If more than two parameters exist, the common
696724
qualifier calculation is recursively applied.
697725
)
@@ -705,7 +733,7 @@ $(H2 $(LNAME2 inout-functions, Inout Functions))
705733
$(TROW $(D inout const) $(LPAREN)= wc$(RPAREN), c, c, wc, wc, wc)
706734
)
707735

708-
$(P The inout in the return type is then rewritten to be the inout matched
736+
$(P The `inout` in the return type is then rewritten to match the `inout`
709737
qualifiers:)
710738

711739
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -749,7 +777,7 @@ $(H2 $(LNAME2 inout-functions, Inout Functions))
749777
)
750778

751779
$(P $(B Note:) Shared types cannot
752-
be matched with inout.
780+
be matched with `inout`.
753781
)
754782

755783
$(H2 $(LNAME2 optional-parenthesis, Optional Parentheses))

0 commit comments

Comments
 (0)