Transitive const sucks

Janice Caron caron800 at googlemail.com
Wed Sep 12 05:16:06 PDT 2007


I'm fair sure that logical const is a real world requirement.

Take the classic example Shape, for example...

const Rectangle r = new Shape();
r.draw();

Whoops! Const-transitivity prevents r.draw() from compiling. Why? Because
Shape has a member variable Raster, and Rectangle.draw() calls
Raster.paintRectangle() or some such, which modifies the state of the
raster.

So what do you do? You could try changing the prototype to
class Rectangle { override invariant draw(Raster raster); }

But now it still won't compile, because the abstract function
Shape.drawwasn't declared like that, so then you have to go back
another step and
change /that/ declaration to:
class Shape { abstract invariant draw(Raster raster); }

It's a solution which doesn't scale.

And there goes encapsulation...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070912/ef76161e/attachment.html>


More information about the Digitalmars-d mailing list