Why do immutable variables need reference counting?
Salih Dincer
salihdb at hotmail.com
Wed Apr 13 04:34:33 UTC 2022
On Monday, 11 April 2022 at 21:48:56 UTC, Ali Çehreli wrote:
> On 4/11/22 08:02, Paul Backus wrote:
>
> > any pointers or references
>
> To add, Salih and I were in an earlier discussion where that
> concept appeared as "indirections."
>
> Ali
I tried the following and I didn't understand one thing: Why is
there no need to use dup when slicing?
```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;
auto s = S!T1(fileName);
fileName[0..3] = "bar";
}/* Results:
*
*T1:
*
source.S!(const(char)[]).S.this
working with foo.txt.
source.S!(const(char)[]).S.~this
working with bar.txt.
*
*T2:
*
source.S!(const(char[])).S.this
working with foo.txt
source.S!(const(char[])).S.~this
working with bar.txt
*
*T3:
*
source.S!(const(char[])).S.this
working with foo.txt
source.S!(const(char[])).S.~this
working with bar.txt
*
*T4
*
Error: slice `fileName[0..3]` is not mutable
*/
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list