On Sun, 29 Nov 2009 13:23:07 +0100, Tomek Sowiñski <j...@ask.me> wrote:

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 ()

There's no problem when pole is const. I assume the problem is the hidden "this" parameter (is it? the message is a bit confusing). Then again, A is implicitly convertible to immutable(A) so there shouldn't be a problem, no? Maybe a compiler bug?

BTW, can someone explain what's exactly the difference between a const and immutable member function? The D page says only about the latter.

Tomek

A is not implicitly castable to immutable(A), only to const(A).
const member functions can be called on any A, unmarked ones only
on A, and immutable ones only on immutable(A).

Basically, immutable is there to give future optimization options,
and immutable objects are not interchangable with mutable objects.
const is the "missing link", allowing one to pass both mutable and
immutable to a function taking const parameters.

An explanation often given on these newsgroups is that const is a
read-only view of the data, whereas immutable is a promise that
the data will never change.

--
Simen

Reply via email to