std.serialization: pre-voting review / discussion

ilya-stromberg ilya-stromberg-2009 at yandex.ru
Tue Aug 20 01:40:57 PDT 2013


On Tuesday, 20 August 2013 at 03:42:48 UTC, Tyler Jameson Little 
wrote:
> On Monday, 19 August 2013 at 18:06:00 UTC, Johannes Pfau wrote:
>> An important question regarding ranges for std.serialization 
>> is whether
>> we want it to work as an InputRange or if it should _take_ an
>> OutputRange. So the question is
>>
>> -----------------
>> auto archive = new Archive();
>> Serializer(archive).serialize(object);
>> //Archive takes OutputRange, writes to it
>> archive.writeTo(OutputRange);
>>
>> vs
>>
>> auto archive = new Archive()
>> Serializer(archive).serialize(object);
>> //Archive implements InputRange for ubyte[]
>> foreach(ubyte[] data; archive) {}
>> -----------------
>>
>> I'd use the first approach as it should be simpler to 
>> implement. The
>> second approach would be useful if the ubyte[] elements were 
>> processed
>> via other ranges (map, take, ...). But as binary data is 
>> usually
>> not processed in this way but just stored to disk or sent over 
>> network
>> (basically streaming operations) the first approach should be 
>> fine.
>
> +1 for the first way.

No, you are WRONG. InputRange is MORE flexible: it can be lazy or 
eager. OutputRange is only eager. As we know, lazy ranges is 
required if it's possible:

On Sunday, 18 August 2013 at 18:26:55 UTC, Dicebot wrote:
> So as a review manager, I think voting should be delayed until 
> API is ready to address lazy range-based work model. No actual 
> implementation is required but
>
> 1) it should be possible to do it later without breaking user 
> code
> 2) library should not make an assumption about implementation 
> being lazy or eager

We can use InputRange like this:

import std.file;
auto archive = new Archive()
Serializer(archive).serialize(object);
//Archive implements InputRange for ubyte[]
write("file", archive);

Another benefit: we can process InputRange. For example, if we 
have
ZipRange zip(InputRange)
function, it's easy to compress data:
write("file", zip(archive));

Another example: we would like to change output xml file and 
filter some data (because we already have it). Or we would like 
to transform output xml to the html web page. No problems:

XmlRange transformXml(InputRange);
write("file", transformXml(archive));

Ideas?


More information about the Digitalmars-d mailing list