[plt-scheme] "appending" to classes rather than extending

From: Jakub Piotr Cłapa (jpc-ml at zenburn.net)
Date: Thu Mar 6 20:56:11 EST 2008

Matthias Felleisen wrote:
> Let me rephrase my answer:
> 
> The purpose of a class is to protect some invariant relationship among 
> the fields and methods. When you don't have the source code of a class, 
> how can  you be sure that "class appending" doesn't violate the 
> relationships -- the original intent (to use an American term) -- of the 
> author(s)?

I guess the answer is: You can't. That's why this is considered fragile. 
On the other hand if it works well enough (i.e. was tested in the 
intended application) than from an engineering point of view it is OK. 
It's difficult prove that you program has no bugs. Either with or 
without class modification.

> For many cases I suspect that class extension suffices.

But it does not work well with object factories (and these may be quite 
common) which return those types you need to augment and it won't fix 
existing instances (e.g. singletons) .

I think the Smalltalk philosophy (where primitives like numbers and 
strigns are also objects) makes it tempting to provide some new methods 
for these. Something like adding a regexp find function to the String 
class ––– you are not likely to break anything since you are not 
mutating object state and the method name collision is also quite unlikely.

> Where it doesn't -- run-time replacement of broken code -- I think the 
> 'no source' doesn't apply and you want an update protocol not a 
> linguistic mechanism.

I guess this is more like replacing/adding methods in a vendor provided 
GUI Button class (or something similar) at the very beginning of your 
code than about upgrading code without interruption (as in Erlang).

-- 
regards,
Jakub Piotr Cłapa


Posted on the users mailing list.