[phobos] Improvement of stream

Andrei Alexandrescu andrei at erdani.com
Mon Jul 5 08:49:34 PDT 2010


On 07/05/2010 09:10 AM, Ellery Newcomer wrote:
> On 07/05/2010 12:37 AM, Andrei Alexandrescu wrote:
>> - Input streams have the read primitive. What is wrong with an input
>> range of ubyte[]? Then accessing front() gives you a buffer and
>> popFront reads in a new buffer.
> How do you handle heterogeneous reads? e.g.
>
> ubyte[] x = new ubyte[](4);
> inp.read(x);
> ubyte[] y = new ubyte[](100);
> inp.read(y);

Good point. With ByChunk you only get to set the size once at 
construction time. A natural extension of that would be to define a 
property bufferSize that you can assign before calling popFront. That 
would allow code like:

inp.bufferSize = 4;
inp.popFront();
process(inp.front);
inp.bufferSize = 100;
inp.popFront();
process(inp.front);

I reckon that just calling read() with different lengths is a bit more 
appealing. Also if you need to save the data you need to call 
inp.front.dup which makes for an extra copy. The question is how often 
you need to do all that versus just getting whatever data is available.

>> - What's the purpose of StreamWrapper?
>
> Having input stream types which aren't runtime type compatible is kind
> of a sucky situation. That's what the interfaces are for, and that's
> what the wrapper provides for e.g. input streams which are structs.

Makes sense. Probably with time we'll need to add interfaces for all 
kinds of ranges.

For wrapping structs in general, I hope we could have a more general 
version similar to BlackHole and WhiteHole - automatic wrapping. Consider:

struct A {
     this(int);
     void foo();
     int bar(string);
}

alias Classify!A ClassA;

ClassA is as if somebody sat down and wrote the following class by hand:

class ClassA {
     private A payload;
     this(int x) { payload = A(x); }
     void foo() { return payload.foo(); }
     int bar(string x) { return payload.bar(x); }
}

Things get more complicated with functions that receive or return type 
A, properties, qualifiers etc. I think Classify would be an excellent 
test of D's introspection capabilities, in addition to being useful in 
practice.


Andrei


More information about the phobos mailing list