Immutable member functions on mutable objects

BCS none at anon.com
Mon Nov 30 14:44:34 PST 2009


Hello Tomek,

> I've got a problem calling an immutable getter on an "ordinary"
> object.
> 
> struct A {
> float _pole;
> float pole() immutable {
> return _pole;
> }
> }
> void main() {
> A a;
> auto x = a.pole;   // Ouch!
> }
> Error: function hello.A.pole () immutable is not callable using
> argument types ()
> 

immutable requiers that the value not be able to change

void main() {
  A a;
  auto x = a.pole;
  a._pole++;
  auto y = a.pole; // pole returns something different than it did last time.
}




More information about the Digitalmars-d-learn mailing list