how is this array subtyping inside struct (bug?) possible?

mw mingwu at gmail.com
Sun Aug 9 21:12:58 UTC 2020


On Sunday, 9 August 2020 at 20:30:35 UTC, Paul Backus wrote:

> Normally, that's not a problem, because writeln takes its 
> arguments by value, so any range you pass to it will be copied, 
> and only the copy will be consumed. However, because you've 
> made your `Filenames` class into an input range using `alias 
> this`, and classes are reference types, consuming a copy of the 
> range also consumes the original.

I also tried this:

```
   foreach (i; 0..3) {
     S* s1 = new S();
     *s1 = s0;
     writeln(s0.fns);
     writeln(s0.fns.array.length);  // *directly* access array
   }
```

The output is:

```
["abc"]
0
[]
0
[]
0
```

I'm *directly* access the underlying array, so why its length 
changed to 0 after writeln?

So a plain array *is* also a range? I've thought (an array's) 
range is a *separate* struct (which wrap the original array). The 
wrapper range can be consumed, but the underlying array should 
stay the same.

I never seen it's mentioned in the doc:

https://dlang.org/spec/arrays.html



More information about the Digitalmars-d mailing list