You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
return s.str; // invalid, scope applies to struct members
725
+
return *s.strPtr; // valid, scope struct member is dereferenced
726
+
return sPtr.str; // valid, struct pointer is dereferenced
727
+
return *sPtr.strPtr; // valid, two pointers are dereferenced
728
+
return sarray[0]; // invalid, scope applies to static array elements
729
+
return sarray[1]; // invalid, ditto
730
+
return darray[0]; // valid, scope applies to array pointer, not elements
731
+
}
732
+
---
733
+
734
+
$(P
735
+
A "`scope` value" is the value of a `scope` variable, or any generated value that is (or could be) allocated on the stack:
736
+
$(UL
737
+
$(LI The address of a function local variable / parameter)
738
+
$(LI A struct member accessed through the implicit `this` parameter)
739
+
$(LI The return value of a $(DDSUBLINK spec/function, ref-functions, Ref Function))
740
+
$(LI The variadic parameter from $(DDSUBLINK spec/function, typesafe_variadic_functions, Typesafe Variadic Functions))
741
+
)
742
+
743
+
When a local variable is assigned a `scope` value, it is inferred `scope`, even when the variable has an explicit type and does not use the `auto` keyword.
744
+
)
745
+
746
+
---
747
+
int* escape(ref int x, int y, scope int* z)
748
+
{
749
+
scope int* w;
750
+
751
+
auto xPtr = &x; // inferred `scope int*`
752
+
int* yPtr = &y; // also inferred `scope int*`
753
+
int* zCopy = z; // inferred `scope int*`
754
+
int* wCopy = w; // inferred `scope int*`
755
+
756
+
return yPtr; // error
757
+
}
758
+
759
+
void variadic(int[] a...)
760
+
{
761
+
int[] x = a; // inferred `scope int[]`
762
+
}
763
+
764
+
struct S
765
+
{
766
+
int x;
767
+
768
+
// this method may be called on a stack-allocated instance of S
769
+
void f()
770
+
{
771
+
int* p = &x; // inferred `scope int* p`
772
+
int* q = &this.x; // equivalent
773
+
}
774
+
}
775
+
---
776
+
777
+
$(P
778
+
$(DDSUBLINK spec/function, scope-parameters, scope parameters) are treated the same as scope local variables,
779
+
except that returning them is allowed when the function has $(DDSUBLINK spec/function, function-attribute-inference, Function Attribute Inference).
780
+
In that case, they are inferred as $(DDSUBLINK spec/function, return-scope-parameters, return scope parameters).
781
+
)
782
+
783
+
$(H3 $(LNAME2 scope-class-var, $(D scope) variables with `class` type))
784
+
$(P
785
+
When used on variables with a `class` type, `scope` signifies the RAII
786
+
(Resource Acquisition Is Initialization) protocol.
650
787
This means that the destructor for an object is automatically called when the
651
788
reference to it goes out of scope. The destructor is called even
652
789
if the scope is exited via a thrown exception, thus $(D scope)
653
790
is used to guarantee cleanup.
654
791
)
655
792
$(P
656
-
If there is more than one $(D scope) variable going out of scope
793
+
When a class is constructed with `new` and assigned to a local `scope` variable,
794
+
it may be allocated on the stack and permitted in a `@nogc` context.
795
+
)
796
+
$(P
797
+
If there is more than one `scope` class variable going out of scope
657
798
at the same point, then the destructors are called in the reverse
658
799
order that the variables were constructed.
659
800
)
660
801
$(P
661
-
$(D scope) cannot be applied to globals, statics, data members, ref
662
-
or out parameters. Arrays of $(D scope)s are not allowed, and $(D scope)
663
-
function return values are not allowed. Assignment to a $(D scope),
664
-
other than initialization, is not allowed.
665
-
$(D Rationale:) These restrictions may get relaxed in the future
666
-
if a compelling reason to appears.
802
+
If there is more than one `scope` class variable going out of scope
803
+
at the same point, then the destructors are called in the reverse
804
+
order that the variables were constructed.
667
805
)
806
+
$(P
807
+
Assignment to a $(D scope) variable with class type,
808
+
other than initialization, is not allowed, because that would complicate
809
+
proper destruction of the variable.
810
+
)
811
+
812
+
---
813
+
class C {}
814
+
815
+
void main() @nogc
816
+
{
817
+
{
818
+
scope c0 = new C(); // allocated on the stack
819
+
scope c1 = new C();
820
+
821
+
c1 = c0; // not allowed
822
+
823
+
// destructor of `c1` and `c0` are called here in that order
0 commit comments