SumType.opAssign

Assigns a value to a SumType.

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 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.

  1. SumType opAssign(T rhs)
    struct SumType(Types...)
    version(StdDdoc)
    ref
    opAssign
    (
    T rhs
    )
  2. SumType opAssign(T rhs)
  3. SumType opAssign(SumType rhs)
  4. SumType opAssign(SumType rhs)

Examples

$(DIVID memory-corruption, $(H3 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
    }
);

Meta