std.file.read implementation contest

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Feb 17 09:37:30 PST 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



More information about the Digitalmars-d mailing list