@@ -43,9 +43,9 @@ $(GNAME Parameter):
43
43
$(I ParameterAttributes)$(OPT) $(GLINK2 type, Type) $(D ...)
44
44
45
45
$(GNAME ParameterAttributes):
46
- $(I InOut)
46
+ $(GLINK InOut)
47
47
$(GLINK2 attribute, UserDefinedAttribute)
48
- $(I ParameterAttributes InOut)
48
+ $(I ParameterAttributes) $(GLINK InOut)
49
49
$(I ParameterAttributes) $(GLINK2 attribute, UserDefinedAttribute)
50
50
$(I ParameterAttributes)
51
51
@@ -646,52 +646,80 @@ $(H2 $(LNAME2 auto-ref-functions, Auto Ref Functions))
646
646
647
647
$(H2 $(LNAME2 inout-functions, Inout Functions))
648
648
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:
652
653
)
653
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
654
654
---
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]; }
656
656
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]; }
658
658
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]; }
660
660
---
661
- )
662
661
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:)
666
664
667
665
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
668
666
---
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]; }
670
668
---
671
669
)
672
670
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.
678
675
)
679
676
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.
683
698
)
684
699
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:)
687
715
688
716
$(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.)
692
720
)
693
721
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
695
723
the matched qualifiers. If more than two parameters exist, the common
696
724
qualifier calculation is recursively applied.
697
725
)
@@ -705,7 +733,7 @@ $(H2 $(LNAME2 inout-functions, Inout Functions))
705
733
$(TROW $(D inout const) $(LPAREN)= wc$(RPAREN), c, c, wc, wc, wc)
706
734
)
707
735
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`
709
737
qualifiers:)
710
738
711
739
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -749,7 +777,7 @@ $(H2 $(LNAME2 inout-functions, Inout Functions))
749
777
)
750
778
751
779
$(P $(B Note:) Shared types cannot
752
- be matched with inout.
780
+ be matched with ` inout` .
753
781
)
754
782
755
783
$(H2 $(LNAME2 optional-parenthesis, Optional Parentheses))
0 commit comments