Thank you!

Tejas notrealemail at gmail.com
Tue Sep 7 05:34:29 UTC 2021


On Tuesday, 7 September 2021 at 05:09:39 UTC, jfondren wrote:
> On Tuesday, 7 September 2021 at 05:01:30 UTC, Tejas wrote:
>> This reminds me of single length string literals being treated 
>> as a character.
>>
>> ```d
>> void main(){
>>      char[5] a = "y";//shouldn't compile, but it does
>>      char[5] b = 'y';
>>      writeln(a);
>>      writeln(b);
>> }
>>
>>
>> Output:
>> y
>> yyyyy
>> ```
>>
>> Was it a good thing that the compiler rewrote the string into 
>> a char in this case? Wouldn't it be better if an error/warning 
>> was provided instead?
>
> This example works with a initialized to "yy" as well, or 
> "yyxx". The chars not provided are initialized to 0 (rather 
> than 255 if the variable weren't initialized at all). This 
> seems like a nice way to reuse a large fixed buffer that 
> initially has small relevant content, like `char[255] line = 
> "hello";`
>
> It doesn't initialize the entire array to 'y', so it's not 
> treating "y" as the same as 'y'.

Oh... that looks more convenient than
```d
char[10] a;
a[0..5] = 'y';
```

Can't even initialize `a` with index `0` at point of declaration 
unless size of definition is same as size of array, basically 
making
```d
char[10] a = [0:'y',1:'y',2:'y',3:'y',4:'y'];
```
impossible.

Agh, maybe that thing needs to exist after all


More information about the Digitalmars-d mailing list