Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!satisfied.elf.com!news.mathworks.com!udel!gatech!ncar!uchinews!news
From: kit@delphi.bsd.uchicago.edu (Kit Transue)
Subject: Modifying String behavior (superclass varies type)
Message-ID: <1995Jan25.220311.18399@midway.uchicago.edu>
Sender: news@uchinews.uchicago.edu (News System)
Organization: University of Chicago -- Academic Information Technologies
Date: Wed, 25 Jan 1995 22:03:11 GMT
Lines: 42

I've run into this problem trying to inherit from a String in VisualWorks.   
It looks like one would have the same problem in VisualAge, and I presume  
it comes up in other Smalltalks as well, at some point or another.

I want to look things up in a dictionary using a case-insensitive compare  
on strings.

If this were C++ <grin>, all I would have to do is derive a class from  
String and define operator= to return String::sameAs.

I try this in Smalltalk--I derive a new class from String.  In the  
comparing protocol I add:

= aString
	^self sameAs: aString!

No other methods are implemented.

Of course, this doesn't work.  When I call "MyString fromString: x",  
String chooses an implementation for itself--in this case a  
ByteString?--and returns one of those.  This thing that it returns isn't a  
MyString, and so it uses String's case-sensitive =.

How on Earth am I supposed to change the behavior of =?  [This is question  
number one.]  I'd really like to be able to treat MyStrings as Strings,  
and am not wild about re-implementing all this behavior.  If I embed a  
String representation in MyString, and supply a doesNotUnderstand method  
that forwards the message to the rep, well, I don't think this would work.   
I don't think I'd get that neat type checking that VW provides.  But worse  
than that, messages like isString would go to Object first, and return  
false incorrectly.

I suppose I could both embed the rep. and derive from String, but that  
seems to get even more unpleasant....


Does it strike anyone else that this variable-class-returned-from-new  
really screws up the inheritance mechanism?  Or have I just missed some  
design paradigm?  [Questions two and three.]

Many thanks in advance,
kit transue
