February 17, 2009
Sean Kelly wrote:
> Andrei Alexandrescu wrote:
>>
>> I totally agree. The useful spec of std.file.read should be "reads the file to exhaustion in a buffer and returns it"
> 
> Okay, that's a fair definition.  So the correct behavior for reading an unbounded stream should be an out of memory error?  This would be entirely reasonable, but any failure conditions should be described as well.

I think that's reasonable and will document it as such. Nice example with /dev/random though :o).

Andrei
February 17, 2009
On Mon, 16 Feb 2009 16:36:52 -0800, Sean Kelly wrote:

> Andrei Alexandrescu wrote:
>> 
>> I totally agree. The useful spec of std.file.read should be "reads the file to exhaustion in a buffer and returns it"
> 
> Okay, that's a fair definition.  So the correct behavior for reading an unbounded stream should be an out of memory error?  This would be entirely reasonable, but any failure conditions should be described as well.

Then why doesn't cp have a warning about copying /dev/random or /dev/ zero?  I think it's enough to specify what it does (read the entire file) and trust that people understand what they are doing when the attempt to read an unbounded stream.  The warning belongs with the stream, not with the simple utility.  It probably only takes once for the developer to screw it up to learn ;)

But I guess it doesn't hurt...

-Steve
February 17, 2009
Sean Kelly wrote:
> Andrei Alexandrescu wrote:
>>
>> I totally agree. The useful spec of std.file.read should be "reads the file to exhaustion in a buffer and returns it"
> 
> Okay, that's a fair definition.  So the correct behavior for reading an unbounded stream should be an out of memory error?  This would be entirely reasonable, but any failure conditions should be described as well.

I thought more about this and a nice solution is to have std.file.read take an optional "up to" parameter:

void[] read(in char[] filename, size_t upTo = size_t.max);

Then you can get a random int with:

int rnd = (cast(int[]) read("/dev/random", 4))[0];

Yum.


Andrei
February 17, 2009
Regarding this really useful part of the std lib, I warmly suggest to also benchmark the final code against a similar operation done in Perl or Python2.x. Such tests very often show performance bugs/problems.

Python code for the test is really simple:

file("filename.txt).read()

Bye,
bearophile
February 17, 2009
On Tue, 17 Feb 2009 21:08:15 +0300, bearophile <bearophileHUGS@lycos.com> wrote:

> Regarding this really useful part of the std lib, I warmly suggest to also benchmark the final code against a similar operation done in Perl or Python2.x. Such tests very often show performance bugs/problems.
>
> Python code for the test is really simple:
>
> file("filename.txt).read()
>
> Bye,
> bearophile

How does it work with /dev/random etc?

1 2
Next ›   Last »