Essence of Dispatch Taking Pharo Booleans as Example Damien Cassou, - - PowerPoint PPT Presentation

essence of dispatch
SMART_READER_LITE
LIVE PREVIEW

Essence of Dispatch Taking Pharo Booleans as Example Damien Cassou, - - PowerPoint PPT Presentation

Essence of Dispatch Taking Pharo Booleans as Example Damien Cassou, Stphane Ducasse and Luc Fabresse W3S01 http://www.pharo.org Objectives Understanding of message passing (late binding) the heart of OOP more an OOP lecture than a


slide-1
SLIDE 1

Essence of Dispatch

Taking Pharo Booleans as Example

Damien Cassou, Stéphane Ducasse and Luc Fabresse

W3S01

http://www.pharo.org

slide-2
SLIDE 2

Objectives

Understanding of message passing (late binding)

  • the heart of OOP
  • more an OOP lecture than a Pharo one

Insight at how beautiful Pharo’s implementation is W3S01 2 / 21

slide-3
SLIDE 3

Context: Booleans

In Pharo, Booleans have a superb implementation!

&, |, not (eager)

  • r:, and: (lazy)

ifTrue:ifFalse:, ifFalse:ifTrue: W3S01 3 / 21

slide-4
SLIDE 4

Three Exercises

  • 1. Implement not (Not)
  • 2. Implement | (Or)
  • 3. What is the goal of these exercises?

W3S01 4 / 21

slide-5
SLIDE 5

Exercise 1: Implement Not

Propose an implementation of Not in a world where:

You have: true, false You only have objects and messages How would you implement the message not?

false not

−> true

true not

−> false

W3S01 5 / 21

slide-6
SLIDE 6

Hint 1: No conditionals

The solution does not use conditionals (i.e., no if)

W3S01 6 / 21

slide-7
SLIDE 7

Hint 2: With Three Classes

The solution uses three classes:

  • Boolean (abstract), True and False

true is the singleton instance of True false is the singleton instance of False W3S01 7 / 21

slide-8
SLIDE 8

Hint 2: Three Classes

W3S01 8 / 21

slide-9
SLIDE 9

Hint 3: How do We Express Choice in OOP?

In OOP , choice is expressed

By defining classes with compatible methods By sending a message to an instance of such class

Example x open

x can be a file, a window, a tool,... The method is selected based on x’s class W3S01 9 / 21

slide-10
SLIDE 10

Implementation of Not in Two Methods

False >> not "Negation −− answer true since the receiver is false." ^ true True >> not "Negation −− answer false since the receiver is true." ^ false

W3S01 10 / 21

slide-11
SLIDE 11

Implementation Hierarchy

W3S01 11 / 21

slide-12
SLIDE 12

Message Lookup is Choosing the Right Method

W3S01 12 / 21

slide-13
SLIDE 13

Boolean Implementation

Boolean is abstract Subclasses are True and False and implement

  • logical operations &, not
  • control structures and:, or:, ifTrue:, ifFalse:, ifTrue:ifFalse:,

ifFalse:ifTrue:

Boolean>>not "Abstract method. Negation: Answer true if the receiver is false, answer false if the receiver is true." self subclassResponsibility

W3S01 13 / 21

slide-14
SLIDE 14

Behavior of Or

true | true −> true true | false −> true true | anything −> true false | true −> true false | false −> false false | anything −> anything

W3S01 14 / 21

slide-15
SLIDE 15

Implementation of Or in Boolean

Boolean >> | aBoolean "Abstract method. Evaluating Or: Evaluate the argument. Answer true if either the receiver or the argument is true." self subclassResponsibility

W3S01 15 / 21

slide-16
SLIDE 16

Implementation of Or in Class False

false | true −> true false | false −> false false | anything −> anything False >> | aBoolean "Evaluating Or −− answer with the argument, aBoolean." ^ aBoolean

W3S01 16 / 21

slide-17
SLIDE 17

Implementation of Or in Class True

true | true −> true true | false −> true true | anything −> true True >> | aBoolean "Evaluating Or −− answer true since the receiver is true." ^ true

W3S01 17 / 21

slide-18
SLIDE 18

Real Implementation of Or in Class True

The object true is the receiver of the message! True>> | aBoolean "Evaluating disjunction (Or) −− answer true since the receiver is true." ^ true So we can write it like the following: True >> | aBoolean "Evaluating disjunction (Or) −− answer true since the receiver is true." ^ self

W3S01 18 / 21

slide-19
SLIDE 19

Or Implementation in Two Methods

W3S01 19 / 21

slide-20
SLIDE 20

Summary

The solution to implement booleans’ operations:

  • does NOT use conditionals (if)
  • lets the receiver decide

Do not ask, tell W3S01 20 / 21

slide-21
SLIDE 21

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/