Why I can't modify a const(char[])[N]?

monkyyy crazymonkyyy at gmail.com
Sun Dec 29 22:44:56 UTC 2024


On Sunday, 29 December 2024 at 21:58:40 UTC, Renato Athaydes 
wrote:
> I expected this to be accepted by the compiler:
>
> ```
> void foo(const(char[]) c) {
>   const(char[])[2] d;
>   d[0] = c;
> }
> ```
>

```d
void foo(const(char)[] c){
   const(char)[][2] d;
   d[0] = c;
}
```

*AND*

```d
void foo(const(char[]) c){
   const(char)[][2] d;
   d[0] = c;
}
```

*AND*

```d
void foo(const(char[]) c) {
   const(char)[][2] temp;
   temp[0]=c;
   const(char[])[2] d=temp;
}
```

compile

> Why is this ok, but not the previous one?

I dont use const if I can help it so idk details; but slices are 
reference types theres probably some theory about how the length 
is spookily making promises about the data.

Maybe slices are the worse of both worlds of being reference and 
value types, according to extreme safetyism?


More information about the Digitalmars-d-learn mailing list