On Thursday, 31 May 2018 at 01:49:42 UTC, DigitalDesigns wrote:
Using setters, we might want to trigger an event when the value changes. But since the getter is used and can modify the event won't be triggered.

This is a bad design.

Here's similar bad design:

---
class Int
{
  public int value;
}
class Holder
{
  private Int _prop = new Int(10);
  public Int prop() { return _prop; }
  public void prop(Int value)
  {
    assert(value.value == 10);
    _prop = value;
  }
}
Holder a = new Holder();
a.prop().value = 9;
---

I broke an invariant and there's no warning about it. D's properties are indeed broken!

Wait, no, that was Java. I mean, C#. Actually, I messed up some -> vs . and missed a couple pointers and it's C++. No, wait, I think it's Dart. Or maybe a new version of Javascript? I can't keep up with ECMA.

The problem is that mutable getters and validating setters don't mix. This is easy enough to fix in your code: change `T getter()` to `const(T) getter()`.

Reply via email to