www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22418] New: Error in documentation on strings


          Issue ID: 22418
           Summary: Error in documentation on strings
           Product: D
           Version: D2
          Hardware: x86_64
                OS: FreeBSD
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dlang.org
          Assignee: nobody puremagic.com
          Reporter: donaldcallen1942 icloud.com

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;
    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.

Oct 18 2021