Using "strcpy" to assign value to dynamic char array

tsbockman thomas.bockman at gmail.com
Wed Nov 3 04:06:55 UTC 2021


On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:
> Joking aside, I liked the nested struct and its opAssign to 
> mimic internal `arr.length = 42` syntax. (I know it involves a 
> potentially expensive delegate but still...)

The nested struct is not needed. UFCS works for setters, too:

```D
void assumedLength(S)(ref S slice, size_t length) {
     if(slice.length >= length)
         slice.length = length;
     else
         assert(false, "Let's not corrupt memory today.");
}

void main() {
   auto arr = [ 1, 2, 3 ];
   arr.assumedLength = 2;
   writeln(arr);
}
```


More information about the Digitalmars-d-learn mailing list