Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!nntp.sei.cmu.edu!cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!torn!nott!cunews!dbuck
From: dbuck@superior.carleton.ca (Dave Buck)
Subject: Re: Please - Some Advice on Language Choice
X-Nntp-Posting-Host: superior.carleton.ca
Message-ID: <DJtAJB.LHo@cunews.carleton.ca>
Sender: news@cunews.carleton.ca (News Administrator)
Organization: Carleton University, Ottawa, Canada
References: <4avbs3$cs1@decaxp.harvard.edu> <4b43ur$14gg@watnews2.watson.ibm.com> <DJsKIr.Dn3@cunews.carleton.ca> <4b4e38$nh9@meaddata.lexis-nexis.com>
Date: Tue, 19 Dec 1995 02:21:11 GMT
Lines: 66

In article <4b4e38$nh9@meaddata.lexis-nexis.com>,
Jeff Nelson <jeffn@meaddata.com> wrote:
>In smalltalk terms I want to be able to make "self" in a
>message look for appropriate messages in children of this object
>before looking in this object itself or it's parents.

Ok, how about a concrete example:

Object subclass: Account
   instanceVariableNames: 'balance'
   classVariableNames: ''
   poolDictionaries: ''

Account>> balance: newBalance
   balance := newBalance.

Account>> balance
   ^balance

Account>> deposit: amount
    self balance: self balance + amount

Account subclass: CheckingAccount
   instanceVariableNames: 'numberOfTransactions'
   classVariableNames: ''
   poolDictionaries: ''

CheckingAccount>>balance: newAmount
   super balance: newAmount.
   self incrementNumberOfTransactions

Now, when we call:
   CheckingAccount new deposit: 50

Assuming that the instance is initialized to have a balance of 0 and
that a method for incrementNumberOfTransactions exists, the methods
will call like this (parentheses indicate which class contains the
method - the first name is the name of the class of the receiver):

CheckingAccount(Account)>>deposit: 50
   CheckingAccount(Account)>>balance  -> 0
   CheckingAccount(CheckingAccount>>balance: 50
      CheckingAccount(Account)>>balance: 50
      CheckingAccount(CheckingAccount)>>incrementNumberOfTransactions

In other words, this works the way you want.  Any time a normal
message is sent in Smalltalk, the method is found by looking in the
object's class then (if it isn't found), searching in each superclass
in turn.  This is true even when a message is send to self from a
method found in a superclass.  The only exception to this is 'super'
which means 'send a message to self but start the lookup in the class
above the one where the current method is defined.'  To really
understand how this works, you have to look at several examples.  Keep
this definition handy.  It's worded very precisely and it works all
the time if you interpret it literally on a word-by-word basis.

David Buck
dbuck@ccs.carleton.ca

_________________________________
| David K. Buck                 |
| dbuck@ccs.carleton.ca         |
| The Object People             |
|_______________________________|
 

