partial initialization of fixed size ("static") arrays

Paul Backus snarwin at gmail.com
Sat Aug 14 23:09:14 UTC 2021


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.


More information about the Digitalmars-d-learn mailing list