View source code
Display the source code in std/sumtype.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.sumtype.SumType.opAssign - multiple declarations

Function SumType.opAssign

Assigns a value to a SumType.

ref SumType opAssign (
  T rhs
);

If any of the SumType's members other than the one being assigned to contain pointers or references, it is possible for the assignment to cause memory corruption (see the ["Memory corruption" example](#memory-corruption) below for an illustration of how). Therefore, such assignments are considered @system.

An individual assignment can be @trusted if the caller can guarantee that there are no outstanding references to any SumType members that contain pointers or references at the time the assignment occurs.

Examples

Memory corruption

This example shows how assignment to a SumType can be used to cause memory corruption in @system code. In @safe code, the assignment s = 123 would not be allowed.

SumType!(int*, int) s = new int;
s.tryMatch!(
    (ref int* p) {
        s = 123; // overwrites `p`
        return *p; // undefined behavior
    }
);

Function SumType.opAssign

Copies the value from another SumType into this one.

ref SumType opAssign (
  ref SumType rhs
);

See the value-assignment overload for details on @safety.

Copy assignment is @disabled if any of Types is non-copyable.

Function SumType.opAssign

Moves the value from another SumType into this one.

ref SumType opAssign (
  SumType rhs
);

See the value-assignment overload for details on @safety.

Authors

Paul Backus

License

Boost License 1.0