std.compress

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Jun 13 06:14:52 PDT 2013


On 6/13/13, Peter Alexander <peter.alexander.au at gmail.com> wrote:
> If it's not obvious from the context, just be explicit.
>
> newdata = std.compression.lz77.compress(data);
>
> Don't force verbosity on everyone just in case someone wants it.

What happens when we get std.compression.lz78 and you end up
accidentally calling compress on with lz77 and expand with lz78?
Pseudocoding:

module deserialize;
import std.compression.lz77;
auto readfile(string filename) { return readFile(filename).expand; }

module serialize;
import std.compression.lz78;  // oops!
void writeFile(T)(T[] data, string filename) { writeFile(filename,
data.compress);  }

Imports are incredibly easy to screw up. But if we used types instead
of global modules then we could not only make our calling code clearer
(and less buggy), but it would also allow us to use package imports so
we can use any compression algorithm:

module deserialize;
import std.compression;  // package import, e.g. imports lz77, lz78, etc modules
auto readfile(string filename) { return filename.readFile.lz77.expand; }

module serialize;
import std.compression;  // package import
void writeFile(T)(T[] data, string filename) {
data.lz77.compress.writeFile(filename); }

"lz77" would be an auto function which takes the buffer and returns a
Lz77 struct that has expand/compress methods.


More information about the Digitalmars-d mailing list