My thoughts & experiences with D so far, as a novice D coder

Vidar Wahlberg vidar.wahlberg at gmail.com
Thu Mar 28 13:24:49 PDT 2013


To follow up with some new woes I'm currently struggling with:
I'm storing some various values in an ubyte array. I discovered 
that it's probably std.bitmanip I wish to use in order to 
"convert" i.e. an int to 4 bytes (although I went first to 
std.conv looking for this feature).
So I have "ubyte[] buffer;", and my second thought is that the 
"append" method 
(http://dlang.org/phobos/std_bitmanip.html#.append) is what I 
want to append values to my ubyte-array (my first thought was 
something like "buffer ~= to!ubyte[](42);", although then I 
forgot about endianness). In the example in the documentation it 
does say "auto buffer = appender!(const ubyte[])();", with no 
explanation as of what "appender" is (I later learned that this 
is from std.array), but just looking a bit up I see that the 
"write" method explained just above use "ubyte[] buffer; 
buffer.write!ubyte(42);", so I assumed that I could use ubyte[] 
myself instead of this "appender" which I thought was some legacy 
code.
So I write some simple test code:
  import std.bitmanip, std.stdio;
  void main() {
   ubyte[] buffer;
   buffer.append!ubyte(42);
  }
Run it through rdmd, and get: 
"core.exception.AssertError@/usr/include/d/std/array.d(591): 
Attempting to fetch the front of an empty array of ubyte".
Just to see what happens I set the size of the buffer 
("buffer.length = 1;") before appending and run it again. Now it 
runs, but instead of appending it behaves like write(), which was 
not exactly what I wanted.

At this time I google for this "appender" used in the example and 
learn that it comes from std.array, so I import std.array and try 
again using "auto buffer = appender!(ubyte[])();", and surely 
enough, now it does append correctly to the buffer. Great, I have 
a solution, so I go back to my project and implement it like I 
implemented it in my test code, but when I compile my project 
after this addition I get a new cryptic error message: "Error: 
__overloadset isn't a template".
After digging a bit I realized that it's because in my project I 
also import std.file, apparently there are some collisions 
between std.bitmanip and std.file. Again it's solvable, but it's 
yet another fight with the language/standard library. I would 
also assume that it's not that uncommon for a module that use 
std.bitmanip to also use std.file, meaning that this error 
potentially may occur often.

A bit on the side: It seems to me as importing std.bitmanip 
somehow adds new properties to my array (".read()" and 
".write()", for example). Not necessarily a bad thing, more of 
"I've not seen this before, I was expecting that I were to 
concatenate the bytes from the conversion to my buffer using ~".


More information about the Digitalmars-d mailing list