Cannot call mutable method on final struct

Christopher Wright dhasenan at gmail.com
Sun Sep 9 13:46:01 PDT 2007


Hey everyone.

I get an error on the following code complaining about the use of 
Box.toString. The message is:
"Error: cannot call mutable method on final struct"

I don't know what that means. I believe it's complaining about const 
stuff and the struct being altered (that is all that makes sense, given 
the message and the fact that it's in a loop), but looking at the 
toString method in std.boxer.Box, it doesn't seem to alter the struct. 
As far as I can tell, the code should be valid.

The workaround is using 'ref Box b' (or 'ref b') in the foreach loop.

---
import std.boxer, std.string, std.stdio;

class C(U...) {
    string value;
    this (U u) {
       value = "";
       foreach (Box b; boxArray(u)) {
          value ~= b.toString;
       }
    }
}

void main () {
    auto c = new C!(int, string, char)(12, "foom", 't');
    writefln(c.value);
}
---

Can anyone here else say whether this is a bug in Box.toString or in 
determining whether a function is mutable?


More information about the Digitalmars-d-learn mailing list