Cannot call mutable method on final struct
    Christian Kamm 
    kamm.incasoftware at shift-at-left-and-remove-this.de
       
    Mon Sep 10 07:32:29 PDT 2007
    
    
  
Christopher Wright wrote:
> New minimal test case:
> ---
> import std.boxer;
> void main () {
>     foreach (b; boxArray(9, 'b')) {
>        b.toString;
>     }
> }
> ---
The problem is that the b defined in the foreach statement is final, thus D
won't let you call non-const methods on it. Boxer.toString is not a const
method.
Same thing without boxer or toString:
---
struct Foo
{
  const void const_bar() {}
  void nonconst_bar() {}
}
void main()
{
  Foo[] foo_arr;
  foo_arr.length = 1;
  foreach(foo; foo_arr)
  {
    foo.const_bar(); // OK
    foo.nonconst_bar(); // Error: cannot call mutable method on final struct
  }
}
---
    
    
More information about the Digitalmars-d-learn
mailing list