Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!godot.cc.duq.edu!newsgate.duke.edu!news.mathworks.com!newsfeed.internetmci.com!in1.uu.net!world!carlg
From: carlg@world.std.com (Carl E Gundel)
Subject: Re: ifTrue:ifFalse: exception handling
Message-ID: <DtB4ov.9rG@world.std.com>
Organization: The World, Public Access Internet, Brookline, MA
X-Newsreader: TIN [version 1.2 PL2]
References: <4q7qg0$t5e@news2.h1.usa.pipeline.com>
Date: Thu, 20 Jun 1996 16:34:07 GMT
Lines: 40

Pat (patcaul@usa.pipeline.com) wrote:
: I have a question which seems extremely simple but that I haven't seen
: discussed elsewhere. 
:  
: The basic problem is to prevent code such as 
:   anObject ifTrue: [.....] ifFalse: [.....] 
: from falling over where anObject is not an instance of class Boolean, i.e.
: not true or false. 

If you are expecting anObject to be a Boolean, then you want it to 'fall 
over' and produce a walkback.  Then you can fix the bug.  It is expensive 
to handle exceptions but most can be easily designed out (and should be).

If you're using Visual Smalltalk you can code for exceptions like so:

    [ anObject ifTrue: [.....] ifFalse: [.....] ]
        on: Error
        do: [.....]

If on the other hand you expect that there may be an additional case, and 
you know what this other object will be (by design), you can do this:

    (aMyObject isKindOf: MyObject)
    ifTrue: [
        "code to handle this case"
    ]
    ifFalse: [
        aMyObject ifTrue: [ ..... ] ifFalse: [ ..... ]
    ]

Or if for some special reason instances of MyObject should masquerade as 
a Boolean (if it solves our problem more elegantly and reads well) we can 
implement #ifTrue:ifFalse: as an instance method of MyObject.

Carl
-- 
------------------------------------------------------------------
 Carl Gundel  carlg@world.std.com  Shoptalk Systems  508-872-5315
 author of Liberty BASIC, a 1996 PC Magazine Awards Finalist!
 http://world.std.com/~carlg/basic.html
