Can someone explain why i can change this immutable variable please?
Daniel Davidson
nospam at spam.com
Wed Oct 9 09:02:42 PDT 2013
On Wednesday, 9 October 2013 at 15:46:29 UTC, Gary Willoughby
wrote:
>
> So why does this give me an error i expect:
>
> import std.stdio;
>
> class Foo
> {
> public void change(string[] name)
> {
> name[0] = "tess";
> writeln(name);
> }
> }
>
> class Bar
> {
> private static immutable string name[] = ["gary"];
>
> public void test()
> {
> auto foo = new Foo();
> foo.change(this.name);
> }
> }
>
> void main(string[] args)
> {
> auto bar = new Bar();
> bar.test();
> }
Maybe this will help explain it:
void main() {
pragma(msg, typeof(Bar.name));
pragma(msg, typeof(Foo.change));
}
will print:
immutable(char[][])
void(string[] name)
The latter is really:
void(immutable(char)[] name)
The former is really:
immutable(immutable(immutable(char)[])[])
So for name the whole kit and kaboodle is immutable and you can
not pass the whole immutable thing into something that is only
immutable at one level.
Thanks
Dan
More information about the Digitalmars-d-learn
mailing list