Restricting super calls to inherited method
Paul Fernhout
pdfernhout at kurtz-fernhout.com
Sat May 19 18:04:54 UTC 2001
Jon-
Wow. Thanks for the snippet!
So it looks like most of the uses are on the class side. Perhaps this
reflects the examples you and Andrew have raised -- relating to the
creation of new instances?
I'm trying this in 3.0. [By the way, I'm not sure the second clause in
the loop "eachClass class ..." is needed. It seems to produce mostly
"fileOutOn:moveSource:toFile:initializing:" which is in Class,
ExternalStructure, and Metaclass. Also the first clause seems to produce
classes examples as well.]
Definitely there are hundreds of uses in there. For example:
MessageNode>>emitIf: stack on: strm value: forValue
| thenExpr thenSize elseExpr elseSize |
thenSize := sizes at: 1.
elseSize := sizes at: 2.
(forValue not and: [(elseSize*thenSize) > 0])
ifTrue: "Two-armed IFs forEffect share a single pop"
[^ super emitForEffect: stack on: strm].
... rest of methods snipped ....
or:
String>>alike: aString
"Answer some indication of how alike the receiver is to the argument,
0 is no match, twice aString size is best score. Case is ignored."
| i j k minSize bonus |
minSize := (j := self size) min: (k := aString size).
bonus := (j - k) abs < 2 ifTrue: [ 1 ] ifFalse: [ 0 ].
i := 1.
[(i <= minSize) and: [((super at: i) bitAnd: 16rDF) = ((aString at: i)
asciiValue bitAnd: 16rDF)]]
whileTrue: [ i := i + 1 ].
[(j > 0) and: [(k > 0) and:
[((super at: j) bitAnd: 16rDF) = ((aString at: k) asciiValue bitAnd:
16rDF)]]]
whileTrue: [ j := j - 1. k := k - 1. ].
^ i - 1 + self size - j + bonus.
I'm not sure yet why either of these methods needs to call "super"
instead of self. But it does looks like a conversion in Squeak would
involve at least a couple hundred (or more) changes to methods, each of
which requires a complex analysis. Frankly, the more I look at these the
more I think using "super" for other than simple inheritance of the same
method is asking for trouble! I know from my own experience it is
tempting to consider using "super" instead of "self" in the thought it
might somehow be more efficient to skip a level of lookup in a subclass
when you know the subclass doesn't (yet) implement the method.
Here's one that definitely is needed as is (a class side one):
Switch class>>newOff
"Answer an instance of me such that the on and off actions are set to
nil
('no action'), and the state is set to 'off'."
^super new initializeOff
Switch class>>new
"Answer an instance of me such that the on and off actions are set to
nil
('no action'), and the state is set to 'off'."
^self newOff
Since Switch class>>new calls "self newOff", calling super in Switch
class>>newOff avoids an endless loop. Still, it would seem a workaround
might be possible with refactoring the initialize process (and some
handwaving :-).
Thanks for helping quantify this!
-Paul Fernhout
Kurtz-Fernhout Software
=========================================================
Developers of custom software and educational simulations
Creators of the Garden with Insight(TM) garden simulator
http://www.kurtz-fernhout.com
Jon Hylands wrote:
>
> On Sat, 19 May 2001 12:04:40 -0400, Paul Fernhout
> <pdfernhout at kurtz-fernhout.com> wrote:
>
> > To quantify this, anyone have a snippet of code to search on calls to
> > the super pseudo-variable which are not the same as the method the call
> > is in? This doesn't seem like something easy to do from the browser.
>
> This isn't exhaustive, but it shows probably most of them... In my 3.1a
> image, I get the following counts:
>
> 198 - instance methods
> 1879 - class methods
>
> ----------------
>
> Object withAllSubclasses do: [:eachClass |
> eachClass selectors do: [:eachSelector |
> | method |
> method := eachClass compiledMethodAt: eachSelector.
> (method sendsToSuper and: [(method messages includes:
> eachSelector) not])
> ifTrue: [Transcript cr; show: eachClass name, '>>',
> eachSelector]].
> eachClass class selectors do: [:eachSelector |
> | method |
> method := eachClass class compiledMethodAt: eachSelector.
> (method sendsToSuper and: [(method messages includes:
> eachSelector) not])
> ifTrue: [Transcript cr; show: eachClass name, '>>',
> eachSelector]]].
>
> ----------------
More information about the Squeak-dev
mailing list
|