Comment #0 by donaldcallen1942 — 2021-10-18T13:56:18Z
Section 12.16 of the Language Reference contains an example:
char[] str1 = "abc"; // error, "abc" is not mutable
char[] str2 = "abc".dup; // ok, make mutable copy
immutable(char)[] str3 = "abc"; // ok
immutable(char)[] str4 = str1; // error, str4 is not mutable
immutable(char)[] str5 = str1.idup; // ok, make immutable copy
The 4th line of this, the assignment of str4, is problematic.
First of all, you can't do the assignment because the first line, the assignment of str1, is in error, as the document says. There is no str1 to assign because of this, which is what the dmd compiler says if you try to compile this code.
Secondly, the comment says "error, str4 is not mutable". That is not correct. The type of str4 is "immutable(char)[] str4", which gives you a mutable str4 with immutable array contents. In other words, if you had a str1 with immutable contents, you *could* assign it to str4, as this little example demonstrates:
import std.stdio;
int main(string[] args) {
immutable char[] str1 = "abc";
immutable(char)[] str4 = str1;
writeln(str4);
return 0;
}
This compiles and runs correctly.
This is a somewhat serious error, in my view, because it further confuses an aspect of the language that is already confusing, the meaning of immutable char[]foo vs immutable(char)[] foo.
I think this entire example needs to be redone, with a clear view of what it is trying to convey. Once that view has been established, I would urge that you illustrate it with code that produces the same errors when compiled as you cite in the comments.
Comment #1 by dlang-bot — 2023-01-30T10:07:43Z
@andraam21 updated dlang/dlang.org pull request #3501 "Fix issue 22418 - Error in documentation on strings" fixing this issue:
- Fix Issue 22418
https://github.com/dlang/dlang.org/pull/3501
Comment #2 by dlang-bot — 2023-01-31T12:02:28Z
dlang/dlang.org pull request #3501 "Fix Issue 22418 - Error in documentation on strings" was merged into master:
- 9fabaae3a5fdaa77af61837ebcf7e50cc7eda4b2 by Andra Maslaev:
Fix Issue 22418
- 59b2b4fb837d35161569ac07257f4091bb98ab37 by Andra Maslaev:
Fix Issue 22418
Added the compiler errors and the difference between immutable(char)[] and immutable(char[])
- 1a82f2b949043c34a74ac901b6bad65406ae514c by Andra Maslaev:
Fix Issue 22418
- b62bd95176246c884026bcf99c1d79d6af426601 by Andra Maslaev:
Fix Issue 22418
- 4cf1c497a1c4d796ecbdd37ba357c29ac47c753b by Andra Maslaev:
Fix Issue 22418
Updated the explanation
- cebbd5a33852169495e5c1f46a9994cd2d3dce11 by Andra Maslaev:
Fix Issue 22418
Highlighted the words and added the missing part
- d4e99b10dd2f38a31633a5b35fce67d2547f96b9 by Andra Maslaev:
Fix Issue 22418
- 5380d269c44f1417cc759df36729b3e915e3e866 by Andra Maslaev:
Fix Issue 22418
white spaces fixed
https://github.com/dlang/dlang.org/pull/3501