Summary: Inconsistent behavior with const/immutable struct
           Product: D
           Version: 2.023
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD

struct Pair {
    immutable uint g1;
    uint g2;

void main() {

void works() {
    Pair[1] stuff;
    stuff[0] = Pair(1, 2);  // Modify immutable by rebinding whole struct.

void broken() {
    Pair stuff;
    stuff = Pair(1, 2);  // Error:  test.broken.stuff cannot modify struct with
immutable members

I'm honestly not sure which of these represents truly correct behavior.  This
will take some debate and/or a language lawyer to resolve.  If you interpret
the statement someVar = Pair(num1, num2); as a rebinding operation, similar to
rebinding class references, then the behavior in works() is correct.  If you
believe that the struct case is fundamentally different because structs are
value types, then the behavior in broken() may be correct.  However, either way
the behavior should be consistent and should not depend on whether you're
modifying a stack variable or an array element.


Reply via email to