Skip to content

Commit 3e7965e

Browse files
committed
Add individual qualifier documentation
1 parent a272807 commit 3e7965e

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

spec/struct.dd

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ $(GNAME Postblit):
590590
is the most inclusive.)
591591
)
592592

593-
($SPEC_RUNNABLE_EXAMPLE_COMPILE
593+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
594594
---
595595
// struct with alias __xpostblit = __postblit
596596
struct X
@@ -638,7 +638,7 @@ void main()
638638
define `this(this)` and don't have fields that transitively define it.
639639
Example:)
640640

641-
($SPEC_RUNNABLE_EXAMPLE_COMPILE
641+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
642642
---
643643
struct X
644644
{}
@@ -673,6 +673,100 @@ struct X
673673
}
674674
---
675675

676+
$(P The following paragraph describes the behavior of the
677+
postblit in the presence of qualifiers:)
678+
679+
$(OL
680+
$(LI `const`. When a postblit is qualified with `const` as in
681+
$(D this(this) const;) or $(D const this(this);) the code is
682+
ill-formed. The postblit is succesfully called on `mutable`
683+
(due to the mutable to const implicit conversion), `const`
684+
and `immutable` objects, but the postblit cannot modify the object
685+
because it regards it as `const`; hence `const` postblits are of
686+
limited usefulness. Example:)
687+
688+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
689+
---
690+
struct S
691+
{
692+
int n;
693+
this(this) const
694+
{
695+
import std.stdio : writeln;
696+
writeln("postblit called");
697+
//++n; // error: cannot modify this.n in `const` function
698+
}
699+
}
700+
701+
void main()
702+
{
703+
S s1;
704+
auto s2 = s1;
705+
const S s3;
706+
auto s4 = s3;
707+
immutable S s5;
708+
auto s6 = s5;
709+
}
710+
---
711+
)
712+
$(LI `immutable`. When a postblit is qualified with `immutable`
713+
as in $(D this(this) immutable) or $(D immutable this(this))
714+
the code is ill-formed. The `immutable` postblit passes the
715+
compilation phase but cannot be invoked. Example:)
716+
717+
---
718+
struct Y
719+
{
720+
// not invoked anywhere, no error is issued
721+
this(this) immutable
722+
{ }
723+
}
724+
725+
struct S
726+
{
727+
this(this) immutable
728+
{ }
729+
}
730+
731+
void main()
732+
{
733+
S s1;
734+
auto s2 = s1; // error: immutable method `__postblit` is not callable using a mutable object
735+
const S s3;
736+
auto s4 = s3; // error: immutable method `__postblit` is not callable using a mutable object
737+
immutable S s5;
738+
auto s6 = s5; // error: immutable method `__postblit` is not callable using a mutable object
739+
}
740+
---
741+
742+
$(LI `shared`. When a postblit is qualified with `shared` as in
743+
$(D this(this) shared) or $(D shared this(this)) solely `shared`
744+
object may invoke the postblit; attempts of postbliting unshared
745+
objects will result in compile time errors:)
746+
747+
---
748+
struct S
749+
{
750+
this(this) shared
751+
{ }
752+
}
753+
754+
void main()
755+
{
756+
S s1;
757+
auto s2 = s1; // error: shared method `__postblit` is not callable using a non-shared object
758+
const S s3;
759+
auto s4 = s3; // error: shared method `__postblit` is not callable using a non-shared object
760+
immutable S s5;
761+
auto s6 = s5; // error: shared method `__postblit` is not callable using a non-shared object
762+
763+
// calling the shared postblit on a shared object is accepted
764+
shared S s7;
765+
auto s8 = s7;
766+
}
767+
---
768+
)
769+
676770
$(P Unions may not have fields that have postblits.)
677771

678772
$(H2 $(LEGACY_LNAME2 StructDestructor, struct-destructor, Struct Destructors))

0 commit comments

Comments
 (0)