Thread overview
[Issue 13710] Invariants not enforced on methods from alias this
Nov 11, 2014
Mark Isaacson
Nov 11, 2014
Don
November 11, 2014
https://issues.dlang.org/show_bug.cgi?id=13710

Mark Isaacson <markisaa@umich.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86_64                      |All
                 OS|Windows                     |All
           Severity|enhancement                 |normal

--
November 11, 2014
https://issues.dlang.org/show_bug.cgi?id=13710

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy@yahoo.com
         Resolution|---                         |INVALID

--- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> ---
I think from angel's comment that discussion, this bug is invalid. There is no method to call, so it cannot hook the invariant, x -= 100 translates directly to machine instructions.

What I would suggest is instead of using alias this, use opDispatch and opBinary to implement inlined functions that forward to the member. Then your invariant will be called. Instead of using alias this for inheritance, use composition.

--
November 11, 2014
https://issues.dlang.org/show_bug.cgi?id=13710

--- Comment #2 from Don <clugdbug@yahoo.com.au> ---
I think there is still a problem. Does it actually make sense to have a class invariant, when you have an alias this?

It seems as though having an 'alias this' is like exposing a public member
variable: once you do this, the class invariant cannot be trusted to be true at
all times.
I feel uneasy about this; it suggests that 'alias this' is bad practice.

It may be worth adding to the documentation at http://dlang.org/contracts.

That states:
"Invariants are used to specify characteristics of a class or struct that
always must be true (except while executing a member function)."

I think that section should explicitly state that direct modification of members in a way which violates the invariant, will not be detected until a member function is called.

--
November 11, 2014
https://issues.dlang.org/show_bug.cgi?id=13710

--- Comment #3 from Steven Schveighoffer <schveiguy@yahoo.com> ---
It's not just with alias this. Any modification of a member variable outside using the methods can result in breaking the invariant.

I think it's not a bad idea to mention the pitfalls, but I don't think alias this is special in that regard.

--