www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13710] New: Invariants not enforced on methods from alias this

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

          Issue ID: 13710
           Summary: Invariants not enforced on methods from alias this
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: markisaa umich.edu

When you do "alias this" and have an invariant, any 
methods that are forwarded to the aliased member do not invoke 
your invariant methods.

This prevents me from writing a really sleek 10-liner to the tune 
of:

struct ValueRestrictedInteger(int lowerBound, int upperBound) {
  int value;
  alias value this;

  this (int rhs) { value = rhs; }

  invariant() {
    assert (value >= lowerBound && value <= upperBound);
  }

  void forDemonstrationOnly() {}
}

unittest {
  ValueRestrictedInteger!(0, 100) x = 0;
  x += 10;
  x -= 100; //This works, but I don't think it should
  x.forDemonstrationOnly(); //This causes the assertion to fire

  ValueRestrictedInteger!(0, 100) y = -100; //This also would hit 
the assertion
}


Forum post on same topic:
http://forum.dlang.org/thread/ijoqlxqlacccwmqnwpdn forum.dlang.org

--
Nov 10 2014