more OO way to do hex string to bytes conversion

Craig Dillabaugh craig.dillabaugh at gmail.com
Tue Feb 6 20:46:12 UTC 2018


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?

Second, while 'auto' is nice, for learning examples I think 
putting the type there is actually more helpful to someone trying 
to understand what is happening. If you know the type why not 
just write it ... its not like using auto saves you any work in 
most cases. I understand that its nice in templates and for 
ranges and the like, but for basic types I don't see any 
advantage to using it.




More information about the Digitalmars-d-learn mailing list