Why do immutable variables need reference counting?

Ali Çehreli acehreli at yahoo.com
Wed Apr 13 15:39:17 UTC 2022


On 4/12/22 21:34, Salih Dincer wrote:

 > I tried the following and I didn't understand one thing: Why is there no
 > need to use dup when slicing?

I don't think I understand you fully.

 > ```d
 > struct S(T) {
 >   T fileName;
 >
 >    this(T fileName) {
 >      this.fileName = fileName;
 >      report();
 >    }
 >
 >    ~this() { report(); }
 >
 >    void report(string func = __FUNCTION__) {
 >      import std.stdio;
 >      writefln!"%s\nworking with %s"(func, fileName);
 >    }
 > }
 > alias T1 = const(char)[];
 > alias T2 = const char[];
 > alias T3 = const(char[]); // T3 == T2
 > alias T4 = immutable char[]; // Not compiling!
 >
 >
 > void main() {
 >    auto fileName = "foo.txt".dup;

In every test case that 'fileName' is char[].

 >    auto s = S!T1(fileName);
 >    fileName[0..3] = "bar";

For that reason, that expression will always succeed. And that's the 
point I tried to make: Without immutable, struct S *must* make a copy in 
order to guarantee that its member will remain the same.

 > }/* Results:

[...]

 >    *
 >    *T4
 >    *
 >    Error: slice `fileName[0..3]` is not mutable

Your code was different because I get an error that I expect: The 
following line fails.

   auto s = S!T4(fileName);

cannot pass argument `fileName` of type `char[]` to parameter 
`immutable(string) fileName`

Exactly! struct S wants immutable (to guarantee that nobody will mutate 
it) but 'fileName' in main is not immutable. You can make it pass in the 
T4 case

a) by making an immutable copy with .idup:

   auto s = S!T4(fileName.idup);

b) by converting 'fileName' to immutable without a copy:

   import std.exception;
   auto s = S!T4(fileName.assumeUnique);
   // But then, the following will fail. Awesome!
   // fileName[0..3] = "bar";

I am amazed everyday how great D is; such a fun engineering tool!

Ali



More information about the Digitalmars-d-learn mailing list