more OO way to do hex string to bytes conversion

rikki cattermole rikki at cattermole.co.nz
Wed Feb 7 03:25:05 UTC 2018


On 06/02/2018 8:46 PM, Craig Dillabaugh wrote:
> On Tuesday, 6 February 2018 at 18:46:54 UTC, H. S. Teoh wrote:
>> On Tue, Feb 06, 2018 at 06:33:02PM +0000, Ralph Doncaster via 
>> Digitalmars-d-learn wrote:
> clip
>>
>> OO is outdated.  D uses the range-based idiom with UFCS for chaining 
>> operations in a way that doesn't require you to write loops yourself. 
>> For example:
>>
>>     import std.array;
>>     import std.algorithm;
>>     import std.conv;
>>     import std.range;
>>
>>     // No need to use .toStringz unless you're interfacing with C
>>     auto hex = "deadbeef";    // let compiler infer the type for you
>>
>>     auto bytes = hex.chunks(2)    // lazily iterate over `hex` by 
>> digit pairs
>>        .map!(s => s.to!ubyte(16))    // convert each pair to a ubyte
>>        .array;            // make an array out of it
>>
>>     // Do whatever you wish with the ubyte[] array.
>>     writefln("%(%02X %)", bytes);
>>
> clip
>> T
> 
> Wouldn't it be more accurate to say OO is not the correct tool for every 
> job rather than it is "outdated".  How would one write a GUI library 
> with chains and CTFE?

But you could with signatures and structs instead ;)


More information about the Digitalmars-d-learn mailing list