www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18143] New: in/out contracts should be implicitly const

https://issues.dlang.org/show_bug.cgi?id=18143

          Issue ID: 18143
           Summary: in/out contracts should be implicitly const
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ibuclaw gdcproject.org

The same already applies for invariants, you shouldn't be able to modify
internal state inside a in/out contract body.


e.g:
---

struct S
{
    int a;

    invariant
    { a = 42; }  // fails, cannot modify const 'this'

    this(int n)
    in
    { a = 42; }  // compiles -> should fail
    out(result)
    { a = 42; }  // compiles -> should fail
    body
    { }
}

---

This is particularly problematic because program behaviour changes in release
mode.

--
Dec 29 2017