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

Meta jared771 at gmail.com
Thu Apr 8 18:06:25 UTC 2021


On Thursday, 8 April 2021 at 18:01:56 UTC, Meta wrote:
> On Thursday, 8 April 2021 at 12:19:29 UTC, WebFreak001 wrote:
>>
>> ```d
>> string to01String(int[] x) @safe
>> {
>>     auto conv = x.to!(ubyte[]); // allocates new array, so 
>> later cast to string is OK
>>     conv[] += '0'; // assume all numbers are 0-9, then this 
>> gives the correct result
>>     return (() @trusted => cast(string)conv)();
>> }
>> ```
>
> The @trusted lambda can also be replaced with 
> [std.exception.assumeUnique](https://dlang.org/library/std/exception/assume_unique.html).

Never mind me, assumeUnique is @system (or at least it's inferred 
as @system), and anyway, you can't implicitly convert 
`immutable(ubyte)[]` to `immutable(char)[]`.


More information about the Digitalmars-d-learn mailing list