Basic Objects Conditionals and Loops Stphane Ducasse stephane
Basic Objects, Conditionals and Loops Stéphane Ducasse stephane. ducasse@inria. fr http: //www. iam. unibe. ch/~ducasse/ Stéphane Ducasse 1
Conditionals and Loops Booleans Basic Loops Overview of the Collection hierarchy— more than 80 classes: (Bag, Array, Ordered. Collection, Sorted. Collection, Set, Dictionary. . . ) Loops and Iteration abstractions Common object behavior S. Ducasse 2
Booleans S. Ducasse 3
Boolean Objects • • • false and true are objects described by classes Boolean, True and False Uniform, but optimized and inlined (macro expansion at compile time) Logical Comparisons &, |, xor: , not • • S. Ducasse a. Boolean. Expr comparison another. Boolean. Expr (1 is. Zero) & false 4
Boolean Hierarchy • • Please open your browser and analyse it How to implement in OO true and false without conditional? Booleean not if. True: True not if. True: S. Ducasse 5 False not if. True:
Boolean Lazy Logical Operators • Lazy Logical operators a. Boolean. Expr and: and. Block • and. Block will only be valued if a. Boolean. Expression is true • • a. Boolean. Expression or: or. Block will only be valued if a. Boolean. Expression is false and: [1 error: 'crazy'] Pr. It-> false and not an error S. Ducasse 6
Conditional are Messages to Boolean Objects • • • S. Ducasse a. Boolean if. True: a. True. Block if. False: a. False. Block a. Boolean if. False: a. False. Block if. True: a. True. Block a. Boolean if. False: a. False. Block Hint: Take care — true is the boolean value and True is the class of true, its unique instance! 7
Why Block Use in Conditional S. Ducasse • Why do conditional expressions use blocks? • Because, when a message is sent, the receiver and the arguments of the message are always evaluated. Blocks are necessary to avoid evaluating both branches. 8
Collections A lot but key abstraction Key iterators S. Ducasse 14
Collections • S. Ducasse Some criteria to identify them – – – Access: indexed, sequential or key-based. Size: fixed or dynamic. Element type: any or well-defined type. Order: defined, defineable or none. Duplicates: possible or not 15
Essential Collection Sequenceable ordered Arrayed. Collection fixed size + key = integer Array any kind of elements Character. Array elements = character String Integer. Array Interval arithmetique progression Linked. List dynamic chaining of the element Ordered. Collection size dynamic + arrival order Sorted. Collection explicit order Bag possible duplicate + no order Set no duplicate + no order Identity. Set identification based on identity Dictionary element = associations + key based Identity. Dictionary key based on identity S. Ducasse 16
Essential Collections: Another View S. Ducasse 17
Some Collection Methods • Will be defined, redefined, optimized or forbidden in the subclasses • Accessing: size, capacity, at: an. Integer put: an. Element Testing: is. Empty, includes: an. Element, contains: a. Block, occurences. Of: an. Element Adding: add: an. Element, add. All: a. Collection Removing: remove: an. Element, remove: an. Element if. Absent: a. Block, remove. All: a. Collection Enumerating (See generic enumerating): do: a. Block, collect: a. Block, select: a. Block, reject: a. Block, detect: a. Block if. None: a. None. Block, inject: avalue into: a. Binary. Block Converting: as. Bag, as. Set, as. Ordered. Collection, as. Sorted. Collection, as. Array, as. Sorted. Collection: a. Block Creation: with: an. Element, with: with: , with: All: a. Collection • • • S. Ducasse 18
Sequenceable Specific (Array) |arr| arr : = #(calvin hates suzie). arr at: 2 put: loves. arr Pr. It-> #( calvin loves suzie) • Accessing: first, last, at. All. Put: an. Element, at. All: an. Index. Collection: put: an. Element • Searching (*: + if. Absent: ): index. Of: an. Element, index. Of: an. Element if. Absent: a. Block • Changing: replace. All: an. Element with: another. Element • Copying: copy. From: first to: last, copy. With: an. Element, copy. Without: an. Element S. Ducasse 19
Keyed. Collection Specific (Dictionary) |dict| dict : = Dictionary new. dict at: 'toto' put: 3. dict at: 'titi' if. Absent: [4]. -> 4 dict at: 'titi' put: 5. dict remove. Key: 'toto'. dict keys -> Set ('titi') • Accessing: at: a. Key, at: a. Key if. Absent: a. Block, at: a. Key if. Absent. Put: a. Block, at: a. Key put: a. Value, keys, values, associations • Removing: remove. Key: a. Key, remove. Key: a. Key if. Absent: a. Block • Testing: include. Key: a. Key • Enumerating: keys. And. Values. Do: a. Block, associations. Do: a. Block, keys. Do: a. Block S. Ducasse 20
Common Shared Behavior S. Ducasse 28
Common Shared Behavior • • • S. Ducasse Object is the root of the inheritance tree Defines the common and minimal behavior for all the objects in the system. Comparison of objects: ==, ~~, =, =~, is. Nil, not. Nil 29
Identity vs. Equality • • • = an. Object returns true if the structures are equivalent (the same hash number) (Array with: 1 with: 2) = (Array with: 1 with: 2) Pr. It-> true == an. Object returns true if the receiver and the argument point to the same object. == should never be overridden. Object>>= an. Object ^ self == an. Object ~= is: not = ~~ is: not == (Array with: 1 with: 2 ) == (Array with: 1 with: 2) Pr. It-> false (Array with: 1 with: 2 ) = (Array with: 1 with: 2) Pr. It-> true • S. Ducasse Take care when redefining =. One should override hash too! 30
Common Behavior: Printing • Print and store objects: print. String, print. On: a. Stream. print. String calls print. On: a. Stream (123 1 2 3) print. String -> ' (123 1 2 3)' Date today print. String -> 'October 5, 1997' S. Ducasse 31
Storing • • store. String, store. On: a. Stream. store. String calls store. On: a. Stream Date today store. String -> '(Date read. From. String: ''10/5/1997'')' Ordered. Collection new add: 4 ; add: 3 ; store. String -> '((Ordered. Collection new) add: 4; add: 3; yourself)’ • S. Ducasse You need the compiler, so for a deployment image this is not convenient 32
read. From. String: recreating Objects • Create instances from stored objects: class methods read. From: a. Stream, read. From. String: a. String • Object read. From. String: '((Ordered. Collection new) add: 4; yourself)' -> Ordered. Collection (4) • S. Ducasse 33
Notifying the Programmer • error: a. String, • does. Not. Understand: a. Message, • halt, halt: a. String, To invoke the debugger Input default. State shift. Pressed if. True: [self halt] • should. Not. Implement Sign of bad design: subclassing • subclass. Responsibility Abstract method • • S. Ducasse 34
Copying in VW • • • S. Ducasse Copying of objects: shallow. Copy, copy shallow. Copy : the copy shares instance variables with the receiver. default implementation of copy is shallow. Copy 35
Copying in VW Object>>copy ^ self shallow. Copy post. Copy Object>>post. Copy ^ self • post. Copy is a hook method • copy is a template method S. Ducasse 36
Summary times. Repeat: , to: do: , Array, Ordered. Collection, Dictionary, Set do: , select: , collect: S. Ducasse 37
- Slides: 25