Cannot call mutable method on final struct

Carlos Santander csantander619 at gmail.com
Sun Sep 9 15:26:42 PDT 2007


Christopher Wright escribió:
> 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?

I don't use D 2.0, but here it goes...

string is const(char)[] or const char[] or something like that... It's const, 
anyway, so you aren't supposed to modify it. Replace "string value" with "char[] 
value".

-- 
Carlos Santander Bernal


More information about the Digitalmars-d-learn mailing list