Daniel Murphy napisał:
> Defining a member function to be immutable doesn't mean it can only access
> immutable member variables, it means it must be called with an immutable
> class reference.
>
> class C
> {
> void fun() immutable {}
> }
>
> void main()
> {
> auto c = new C();
> c.fun(); // error
> auto i = new immutable C();
> i.fun(); // ok
> }
>
> For nested functions, the reference is to the enclosing function's stack
> frame. What does it mean to have this be immutable? Maybe this makes sense
> if EVERY variable in the enclosing stack frame is immutable?
Good point. I think the concept of immutable nested functions is still a solid
improvement, even with requiring every variable of the enclosing stack frame to
be immutable.
> Is there any reason you couldn't just use static nested pure functions?
>
> void main()
> {
> static pure Node* grow_tree(int breadth) // strongly pure
> {
> ....
> }
> immutable Node* tree = grow_tree(...);
> }
I fear one would need to tediously add arguments for *every* ingredient in the
scope to be able to cook them inside. And if an ingredient turns out not
necessary as the software evolves, there's the tedium of removing it from the
nested function signature, which -- let's face it -- nobody does. Initializing
immutable structures is fairly common and lack of a convenient way to do it has
been recognized as one of the shortcomings of the const system. It can and
should be solved.
--
Tomek