Thanks for your idea.

On Monday, 18 December 2017 at 05:08:24 UTC, Jonathan M Davis wrote:
         ubyte[] makeCMDPacket(RCPCmd cmd, in ubyte[] data)
         {
             this.preamble = RCPPKT_PRE;
             this.hdr.msgtype = RCPMSG_CMD;
             this.hdr.cmdcode =  cast (ubyte) cmd;
             this.hdr.len = 0xffff & data.length;

Why are you using & instead of simply casting to ushort? Casting would be more idiomatic. Or you can use to!ushort if you want to verify the size at runtime and have a ConvException thrown if the value is too large.


I'm using & because I'm using a C-Style. and the length needs to be packed into 2bytes, for casting, I don't know what will happen if data.length is greater. In C & C++, this kind of the cast will cause errors. I don't know what D will do for this.

```

It's basically a C-style code, and it works perfect, But I know that this is not a "D-way", So, any suggestions?

I'd suggest that you check out std.bitmanip if you haven't.

https://dlang.org/phobos/std_bitmanip.html

append, peek, read, and write in particular are useful if you're putting various integral values into an array of ubyte[] or reading them from it. Glancing over what you have, I'm pretty sure that all of the bitshifts and bitwise &'s and can be removed by using append or write to write the data to the array and peek or read to read from it.

Also, as far as D style goes, variables and enum values are usually given camelCase names, whereas you've named your enum values in all uppercase and your variables names in all lowercase, and you've underscores in the middle of names. But of course, you can do what you want on that. It's just that if you make any libraries public (e.g. on code.dlang.org), it's better to follow the D naming guidelines for anything in the public API:

https://dlang.org/dstyle.html

- Jonathan M Davis

Yeah, thanks for the suggestion, I will check bit manipulation more in detail. I just heard of that, but I just did a misunderstanding that I think that is for packing and unpacking of bits.

Reply via email to