Skip to content

Commit 3927be8

Browse files
authored
[spec/function] Improve ref and auto (ref) function docs (#3559)
* [spec/function] Improve ref and auto (ref) function docs Make ref function example runnable & mention `return ref` parameters. Make it clearer Auto functions don't have to use `auto`, and they trigger attribute inference. Reword auto ref functions. * Fix closing P
1 parent 2a069fa commit 3927be8

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

spec/function.dd

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -645,16 +645,26 @@ $(H2 $(LNAME2 ref-functions, Ref Functions))
645645
(whereas an expression formed by calling a non-`ref` function is an rvalue).
646646
)
647647

648+
$(SPEC_RUNNABLE_EXAMPLE_RUN
648649
---
650+
int *p;
651+
649652
ref int foo()
650653
{
651-
auto p = new int(2);
654+
p = new int(2);
652655
return *p;
653656
}
654-
...
655-
int i = foo(); // i is set to 2
656-
foo() = 3; // reference returns can be lvalues
657+
658+
void main()
659+
{
660+
int i = foo();
661+
assert(i == 2);
662+
663+
foo() = 3; // reference returns can be lvalues
664+
assert(*p == 3);
665+
}
657666
---
667+
)
658668

659669
$(P Returning a reference to an expired function context is not allowed.
660670
This includes local variables, temporaries and parameters that are part
@@ -669,7 +679,8 @@ ref int sun()
669679
}
670680
---
671681

672-
$(P A `ref` parameter may not be returned by `ref`.)
682+
$(P A `ref` parameter may not be returned by `ref`, unless it is
683+
$(RELATIVE_LINK2 return-ref-parameters, `return ref`).)
673684
---
674685
ref int moon(ref int i)
675686
{
@@ -685,8 +696,7 @@ $(H2 $(LNAME2 auto-functions, Auto Functions))
685696
)
686697

687698
$(P An auto function is declared without a return type.
688-
If it does not already have a storage class, use the
689-
$(D_KEYWORD auto) storage class.
699+
Auto functions can use any valid $(GLINK2 declaration, StorageClass), not just `auto`.
690700
)
691701

692702
$(P If there are multiple $(I ReturnStatement)s, the types
@@ -697,25 +707,37 @@ $(H2 $(LNAME2 auto-functions, Auto Functions))
697707
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
698708
---
699709
auto foo(int x) { return x + 3; } // inferred to be int
700-
auto bar(int x) { return x; return 2.5; } // inferred to be double
710+
pure bar(int x) { return x; return 2.5; } // inferred to be double
701711
---
702712
)
703713

714+
$(NOTE Return type inference also triggers
715+
$(RELATIVE_LINK2 function-attribute-inference, attribute inference).)
716+
717+
704718
$(H2 $(LNAME2 auto-ref-functions, Auto Ref Functions))
705719

706-
$(P Auto ref functions infer their return type just as
720+
$(P Auto ref functions can infer their return type just as
707721
$(RELATIVE_LINK2 auto-functions, auto functions) do.
708722
In addition, they become $(RELATIVE_LINK2 ref-functions, ref functions)
709-
if all return expressions are lvalues,
710-
and it would not be a reference to a local or a parameter.)
723+
if all of these apply:)
724+
725+
* All expressions returned from the function are lvalues
726+
* No local variables are returned
727+
* Any parameters returned are reference parameters
728+
* Each returned expression must implicitly convert to an lvalue of the deduced return type
711729

712730
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
713731
---
714732
auto ref f1(int x) { return x; } // value return
715733
auto ref f2() { return 3; } // value return
716734
auto ref f3(ref int x) { return x; } // ref return
717735
auto ref f4(out int x) { return x; } // ref return
718-
auto ref f5() { static int x; return x; } // ref return
736+
auto ref f5()
737+
{
738+
static int x;
739+
return x; // ref return
740+
}
719741
---
720742
)
721743

@@ -729,16 +751,16 @@ $(H2 $(LNAME2 auto-ref-functions, Auto Ref Functions))
729751
auto ref f3(ref int x, ref double y)
730752
{
731753
return x; return y;
732-
// The return type is deduced to double, but cast(double)x is not an lvalue,
733-
// then become a value return.
754+
// The return type is deduced to be double, but cast(double)x is not an lvalue,
755+
// so f3 has a value return.
734756
}
735757
---
736758
)
737759

738-
$(P Auto ref function can have explicit return type.)
760+
$(P Auto ref functions can have an explicit return type.)
739761

740762
---
741-
auto ref int (ref int x) { return x; } // ok, ref return
763+
auto ref int bar(ref int x) { return x; } // ok, ref return
742764
auto ref int foo(double x) { return x; } // error, cannot convert double to int
743765
---
744766

0 commit comments

Comments
 (0)