Skip to content

Commit 5c41a68

Browse files
authored
Fix Issue 22418 - Error in documentation on strings (#3501)
* immutable strings * indentation error at immutable strings at str1 * Fix Issue 22418 * Fix Issue 22418 Added the compiler errors and the difference between immutable(char)[] and immutable(char[]) * Fix Issue 22418 * indentation * Fix Issue 22418 * Fix Issue 22418 Updated the explanation * Fix Issue 22418 Highlighted the words and added the missing part * Fix Issue 22418 * Fix Issue 22418 white spaces fixed
1 parent 8ab7617 commit 5c41a68

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

spec/arrays.dd

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,11 +1017,12 @@ $(H3 $(LNAME2 strings, Strings))
10171017

10181018
$(SPEC_RUNNABLE_EXAMPLE_FAIL
10191019
---------
1020-
char[] str1 = "abc"; // error, "abc" is not mutable
1021-
char[] str2 = "abc".dup; // ok, make mutable copy
1020+
char[] str1 = "abc"; // error, cannot implicitly convert expression `"abc"` of type `string` to `char[]`
1021+
char[] str2 = "abc".dup; // ok, makes mutable copy
10221022
immutable(char)[] str3 = "abc"; // ok
1023-
immutable(char)[] str4 = str1; // error, str4 is not mutable
1024-
immutable(char)[] str5 = str1.idup; // ok, make immutable copy
1023+
immutable(char)[] str4 = str2; // error, cannot implicitly convert expression `str2` of type `char[]` to `string`
1024+
immutable(char)[] str5 = str3; // ok, makes a mutable str5 with immutable aray contents
1025+
immutable(char)[] str6 = str2.idup; // ok, makes immutable copy
10251026
---------
10261027
)
10271028

@@ -1031,14 +1032,31 @@ immutable(char)[] str5 = str1.idup; // ok, make immutable copy
10311032

10321033
$(SPEC_RUNNABLE_EXAMPLE_FAIL
10331034
---------
1034-
char[] str1 = "abc"; // error, "abc" is not mutable
1035-
char[] str2 = "abc".dup; // ok, make mutable copy
1035+
char[] str1 = "abc"; // error, cannot implicitly convert expression `"abc"` of type `string` to `char[]`
1036+
char[] str2 = "abc".dup; // ok, makes mutable copy
10361037
string str3 = "abc"; // ok
1037-
string str4 = str1; // error, str4 is not mutable
1038-
string str5 = str1.idup; // ok, make immutable copy
1038+
string str4 = str2; // error, cannot implicitly convert expression `str2` of type `char[]` to `string`
1039+
string str5 = str3; // ok, makes a mutable str5 with immutable aray contents
1040+
string str6 = str2.idup; // ok, makes immutable copy
10391041
---------
10401042
)
10411043

1044+
$(P
1045+
The type $(D immutable(char)[]) represents an array of $(D immutable char)s. However, the reference to the string is
1046+
mutable. If the the reference to the string needs to be immutable as well it can be declared $(D immutable char[])
1047+
or $(D immutable string):
1048+
)
1049+
1050+
---
1051+
immutable char[] s = "foo";
1052+
s[0] = 'a'; // error, s refers to immutable data
1053+
s = "bar"; // error, s is immutable
1054+
1055+
immutable(char)[] s = "hello";
1056+
s[0] = 'b'; // error, s[] is immutable
1057+
s = null; // ok, s itself is not immutable
1058+
---
1059+
10421060
$(P $(CODE char[]) strings are in UTF-8 format.
10431061
$(CODE wchar[]) strings are in UTF-16 format.
10441062
$(CODE dchar[]) strings are in UTF-32 format.

0 commit comments

Comments
 (0)