Booleans and Conditions

Damien Cassou, Stéphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

Booleans

In Pharo, booleans have nothing special

Eager and Lazy Logical Operators

   false & (1 error: 'crazy')
   -> an error
   false and: [ 1 error: 'crazy' ]
   -> false "no error!"

Conditionals

In Pharo, traditional conditional (if, else, while) are messages sent to boolean or block objects

Yes ifTrue:ifFalse: is a message!

Weather isRaining
   ifTrue: [ self takeMyUmbrella ]
   ifFalse: [ self takeMySunglasses ]

Boolean Implementation

Boolean Hierarchy

More details in a future lecture (The Essence of Dispatch)

Conditionals: ifTrue: and ifTrue:ifFalse:

ifTrue: [ ] and ifTrue: [ ] ifFalse: [ ] are two different messages

forceItalicOrOblique
   self slantValue = 0 
      ifTrue: [ slantValue := 1 ]
fullName isEmptyOrNil
   ifTrue: [ 'FirstnameLastname' translated ]
   ifFalse: [ fullName ].

Conditionals: ifFalse: and ifFalse:ifTrue:

ifFalse: [ ] and ifFalse: [ ] ifTrue: [ ] are two different messages

Conditionals: ifEmpty: ifNotEmpty:

myProtocol 
   ifEmpty: [ 'As yet unclassified' ]
self listItems 
   ifNotEmpty: [ :aList | aList at: index ]

Summary

/