Booleans and Conditions Damien Cassou, Stphane Ducasse and Luc - - PowerPoint PPT Presentation

booleans and conditions
SMART_READER_LITE
LIVE PREVIEW

Booleans and Conditions Damien Cassou, Stphane Ducasse and Luc - - PowerPoint PPT Presentation

Booleans and Conditions Damien Cassou, Stphane Ducasse and Luc Fabresse W2S08 http://www.pharo.org Booleans true is the unique instance of class True false is the unique instance of class False In Pharo, booleans have nothing special


slide-1
SLIDE 1

Booleans and Conditions

Damien Cassou, Stéphane Ducasse and Luc Fabresse

W2S08

http://www.pharo.org

slide-2
SLIDE 2

Booleans

true is the unique instance of class True false is the unique instance of class False

In Pharo, booleans have nothing special

& | not

  • r: and: (lazy)

xor: ifTrue:ifFalse: ifFalse:ifTrue: ... W2S08 2 / 11

slide-3
SLIDE 3

Eager and Lazy Logical Operators

false & (1 error: 'crazy')

−> an error

the argument (1 error: ’crazy’) is executed because this is a

non lazy operator false and: [ 1 error: 'crazy' ]

−> false "no error!"

the argument [1 error: ’crazy’] is not executed because it is

not necessary

W2S08 3 / 11

slide-4
SLIDE 4

Conditionals

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

W2S08 4 / 11

slide-5
SLIDE 5

Yes ifTrue:ifFalse: is a message!

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

Conceptually ifTrue:ifFalse: is a message sent to an object:

a boolean!

Heavily optimised by the compiler W2S08 5 / 11

slide-6
SLIDE 6

Boolean Implementation

true is the unique instance of the class True false is the unique instance of the class False

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

W2S08 6 / 11

slide-7
SLIDE 7

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 ].

W2S08 7 / 11

slide-8
SLIDE 8

Conditionals: ifFalse: and ifFalse:ifTrue:

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

W2S08 8 / 11

slide-9
SLIDE 9

Conditionals: ifEmpty: ifNotEmpty:

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

Notice that when the receiver is not empty we get it as

argument

No need to ask it again W2S08 9 / 11

slide-10
SLIDE 10

Summary

Booleans are real objects Some conditionals are messages sent to Booleans W2S08 10 / 11

slide-11
SLIDE 11

A course by and in collaboration with

Inria 2016 Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France https://creativecommons.org/licenses/by-nc-nd/3.0/fr/