Bug 9408 – invariant should be non-const by default and settable to const

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-01-26T21:08:00Z
Last change time
2016-08-27T23:03:47Z
Keywords
pull
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2013-01-26T21:08:35Z
class C1 { int x; invariant() const { x = 1; // not ok } } class C2 { int x; invariant() { x = 1; // ok } } See also https://github.com/D-Programming-Language/dmd/pull/1073, and discussion http://forum.dlang.org/thread/jvh87s%241gl6%[email protected]
Comment #1 by andrej.mitrovich — 2013-01-26T21:45:00Z
Comment #2 by bugzilla — 2013-02-03T11:20:58Z
I think this change is a step backwards.
Comment #3 by bearophile_hugs — 2013-02-03T11:43:10Z
(In reply to comment #2) > I think this change is a step backwards. I think in this discussion there are two main sides: Some people want to write D code right now, so they don't want to fight too much with problems caused by const. And they like "freedom". Often this side is also associated with the "worse is better" language design philosophy. They other camp reads the texts written by the creator of the Eiffel language, that explain what Contracts are and what they are meant to. For them a precondition or an invariant should never modify the state of the struct/class. And the language should enforce this, to avoid bugs. They accept less a bit less convenience for stronger guarantees and "cleaner" code. Mathematics-oriented people are often on this side. D language has introduced const/invariant, but it's so strong that sometimes you don't want or your can't use it. So sometimes D programmers don't want to eat their own dog food. I generally prefer a more "clean" style of coding, because from experience I have seen that the amount of time wasted making a fussy type system happy, is often paid later in less bugs, that otherwise waste far more of my time. But in the end I sympathize with both camps, for different reasons. Both are partially right. In the end a static analysis tool can just enforce all invariants to be tagged with "const", so the end result is not too much different.
Comment #4 by andrej.mitrovich — 2014-02-03T06:09:05Z
Unassigning myself until there's a consensus.
Comment #5 by andrej.mitrovich — 2016-08-27T23:03:47Z
I'm closing this as invalid unless someone comes up with a DIP with all the pro's and con's. When looking at it from a safety point of view I'd rather not have someone accidentally modify a variable in an invariant of all places. Imagine a debug build where a variable was assigned to and made an app work fine, and then a -release build was deployed without the assignment and causing hard to track bugs.