Is D right for me?

Denis Koroskin 2korden at gmail.com
Tue Oct 12 04:04:28 PDT 2010


On Tue, 12 Oct 2010 02:32:55 +0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 10/11/2010 12:38 PM, Daniel Gibson wrote:
>> But parts of phobos are deprecated or will be deprecated and there still
>> is no alternative for them.
>> That may prevent people from writing "real" projects in D2 (or D at all)
>> - who wants to use classes that will be deprecated soon?
>> Sure, that old stuff will not be removed and can still be used, but I
>> personally feel a bit uncomfortable with using deprecated code.
>
> Agreed. Maybe this is a good time to sart making a requirements list for  
> streams. What are the essential features/feature groups?
>
> Andrei

For me, I/O should be scalable (and thus support async operations) so I  
came up with my own implementation.
I've tried building it on top of std.concurrency, but it doesn't scale  
either. So, once again, I had to implement my own message passing  
mechanism. I can implement existing std.concurrency interface on top of my  
own one without sacrificing anything, but not vice-versa).	

Classes implemented so far: FileStream (file i/o), MemoryStream (e.g.  
async memcpy) and SocketStream.

None of the streams support range interface explicitly (by design).  
Instead, range interface can be achieved by StreamReader (InputStream) and  
StreamWriter (OutputStream) adaptors.

Here it is in case you want to take a look and borrow ideas:

Stream: http://bitbucket.org/korDen/io/src/tip/io/stream.d
Mailbox: http://bitbucket.org/korDen/io/src/tip/io/mailbox.d
AsyncRequest: http://bitbucket.org/korDen/io/src/tip/io/async.d

Unlike std.concurrency, it is easy (and encouraged) to have as many  
mailboxes as you need. Mailboxes can forward events to other mailboxes,  
e.g. so that you can poll() only one mailbox (and not every single one of  
them). And the main difference from std.concurrency is that it allows  
event processing in a different thread context. For example, you can  
process network message as soon as it arrives (which is done in a  
background thread), parse it and then dispatch to main thread. This is how  
my HttpRequest (which uses SocketStream) works.

Here is an example:

import io.http;
import io.mailbox;

import std.stdio;
import std.file;

void main()
{
	auto host = "нигма.рф"; // supports international domain names, too

	auto connection = new HttpConnection(host);

version (Wait) {
         auto request = connection.execute(new HttpRequest(host, "/"));
	request.wait(); // blocks until completed
	std.file.write("out.html", request.response.contents);
} else {
	// use thread-unique mailbox for event handing, similar to  
std.concurrency.getTid()
         connection.execute(new HttpRequest(host, "/"), mailbox);

	bool done = false;
	void onComplete(HttpResponseRequest request)
	{
		std.file.write("out.html", request.response.contents);
		done = true;
	}
	
	mailbox.registerHandler!(HttpResponseRequest)(&onComplete);
	
version (Loop) {
	while (!done) {
		mailbox.poll();	// doesn't block
		// do something useful (e.g. show progress bar) while not done
	}
} else {
	mailbox.poll(long.max);
	assert(done);
}
}
	connection.close();
}


More information about the Digitalmars-d mailing list