stream.getc() doesn't recognize eof

Regan Heath regan at netmail.co.nz
Thu Mar 13 10:19:58 PDT 2008


Brian White wrote:
>> So, because c == char.init and nothing has modified it, the path which 
>> calls readBlock will return char.init when EOF is reached.
> 
> Ah, thanks!
> 
> I must say that this technique worries me somewhat.  "readBlock" is an 
> abstract function definable by any derived class and I don't believe 
> that "c must remain unchanged where data is not stored" is a defined 
> output requirement of that method.

Good point, might be safer to check for the 0 return and set c to 
char.init explicitly.

You comment did get me thinking... Is there some way of expressing the 
requirement using design by contract?  I think the answer is, not 
easily, you'd have to do something like:

// the problem being that we need a global to copy the input buffer into
// and it could be potentially huge.
// when really all we want is some way to detect whether
// data was written to that address _at all_
byte* buffer_in;

abstract size_t readBlock(void* buffer, size_t size)
in {
   buffer_in = malloc(size);
   memcpy(buffer_in, buffer, size);
}
out (result) {
   assert(result > 0 ||
         (result == 0 && memcmp(buffer_in, buffer, size) == 0));
}
/* note, no body, therefore function is still 'abstract' */

All that assuming it is legal to specify in/out contracts on an abstract 
method without a body.

It should be possible, it would simply follow the same rules given for 
inheritance here under "In, Out and Inheritance":
http://www.digitalmars.com/d/1.0/dbc.html

Regan


More information about the Digitalmars-d-learn mailing list