On Saturday, 14 August 2021 at 14:04:47 UTC, kdevel wrote:
~~~
   char [7] d7 = "x"; // okay

   string s = "x";
   char [7] c7 = s; // throws RangeError
~~~

What justifies that the compiler behaves differently on two terms ('s', '"x"') which are of equal size, type, length and value?

Literals in D can have different types in different contexts. For example:

```d
byte b = 16; // 16 is treated as a byte literal
int n = 16; // 16 is treated as an int literal
b = n; // Error: cannot convert int to byte
```

Similarly, the string literal `"x"` can be treated either as a `string` (a dynamic array of `immutable(char)`) or as a static array of `char`, depending on the type of variable it's assigned to.

Reply via email to