booleans and conditions
play

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


  1. Booleans and Conditions Damien Cassou, Stéphane Ducasse and Luc Fabresse W2S08 http://www.pharo.org

  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 � or: and: (lazy) � xor: � ifTrue:ifFalse: � ifFalse:ifTrue: � ... W2S08 2 / 11

  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

  4. Conditionals In Pharo, traditional conditional (if, else, while) are messages sent to boolean or block objects W2S08 4 / 11

  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

  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

  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

  8. Conditionals: ifFalse: and ifFalse:ifTrue: ifFalse: [ ] and ifFalse: [ ] ifTrue: [ ] are two different messages W2S08 8 / 11

  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

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

  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/

Recommend


More recommend