Object As Success

last modified: February 10, 2003

Therefore, define ObjectAsSuccess, and create a new Failure class. Any method result that is not of class Failure is by definition a successful result.

Example methods

Object>ifSuccess: aBlock
Object>ifFailure: aBlock
Object>ifSuccess: successBlock ifFailure: failureBlock
Object>isSuccess
Object>isFailure
Failure>ifSuccess: aBlock
Failure>ifFailure: aBlock
Failure>ifSuccess: successBlock ifFailure: failureBlock
Failure>isSuccess
Failure>isFailure

Example code

|result |
self rules do: [ :each |
        result := self fireRule: each with: self.
        result ifSuccess: [^result].
        ].
^ Failure onError: 'No matching rule'.

How does this compare to throwing an exception on failure? One difference is whether you're most likely to just stop processing and bail out (in which case exceptions look cleaner), or to try to continue or repair things on the spot (in which case lots of catch/onExceptionDo: blocks become tedious). Also, I'd be inclined to add a data field to the Failure class so that you can pass back some clue as to why the failure occurred. --SteveFreeman


CategorySuccess


Loading...