Is there a more elegant way to do this in D?

Imperatorn johan_forsberg_86 at hotmail.com
Thu Apr 8 22:20:41 UTC 2021


On Thursday, 8 April 2021 at 22:02:47 UTC, Alain De Vos wrote:
> I resume in the 4 ways presented,
>
> import std;
> void main(){
> 	auto a=[1,0,1,1,1,0,1,0,1,1,1,0];
> 	
> 	string s = format!"%-(%s%)"(a);
> 	writeln(s);
> 	
> 	dchar[12] b = a.map!(to!string).joiner.array;
> 	writeln(b);
> 	
> 	auto conv = a.to!(ubyte[]);
>         conv[]+='0';
>        writeln(cast(string)conv);
>
>         auto r = a.map!(i => cast(char)(i + '0'));
> 	writeln(r);
> }
>
> Why do the last two ways need "auto" as type ?

auto is not a type, it's inferring the type, like var in C#. It's 
just convenient, saves you a few keystrokes.


More information about the Digitalmars-d-learn mailing list