[fpc-devel] A suggestion about streams

Michael Van Canneyt michael at freepascal.org
Fri Jun 30 16:46:48 CEST 2006



On Fri, 30 Jun 2006, Sergei Gorelkin wrote:

> Hello,
>
> Currently, TStream.Read and TStream.Write methods are abstract.
> This means that each and every descendant of TStream must override these
> methods with their implementation.
>
> FCL has many read-only or write-only TStream descendants, which
> override Read or Write methods just to indicate that method is
> unusable, i.e. raise some kind of exception.
>
> Why not implement TStream.Read and TStream.Write like this:
>
> procedure TStream.Read(...);
> begin
>  raise EStreamError.CreateFmt('Reading is not supported by %s',
>                               [ClassName]);
> end;
>
> Since ClassName will be the actual name of the class which executed
> the call, such implementation will give correct and meaningful error
> message for every descendant class.
>
> Then, all FCL code can be cleaned from similar procedures.
>
> What is your opinion? It looks that such modification will not cause
> any incompatibilities with existing code. And if you are not against,
> I can do the required patching.

There is a difference for the implementor of streams:
By keeping the method abstract, you FORCE the implementation of a method
in a descendent.

The compiler will warn you if you try to instantiate a class which has an
abstract method. If you implement the base method as virtual method, you
will lose this warning, but you will not be aware that you have forgotten
to override a method.

Michael.



More information about the fpc-devel mailing list