1 / 36

CLOS

CLOS. CSC 358/458 6.3.2003. Outline. Homework #8 CLOS Basic features OO programming Examples. Homework #8. Monkey Student. CLOS. Adds object-oriented programming to Lisp OOP is a choice like C++. CLOS features. Built-in classes integrated with type system

leilanib
Download Presentation

CLOS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CLOS CSC 358/458 6.3.2003

  2. Outline • Homework #8 • CLOS • Basic features • OO programming • Examples

  3. Homework #8 • Monkey • Student

  4. CLOS • Adds object-oriented programming to Lisp • OOP is a choice • like C++

  5. CLOS features • Built-in classes integrated with type system • User-defined classed also part of type system • Multiple inheritance • Class variables (static) and instance variables • Programmer-selected method combination • Multiple dispatch

  6. defclass • creates a new class • supply • name • superclasses • slot specifiers • class options

  7. Example (defclass submarine (boat) (speed depth))

  8. make-instance • Creates an instance of a class • Example • (make-instance 'submarine)

  9. slot-value • Allows access to defined slot • instance variables • Example • (setf s1 (make-instance 'submarine)) • (setf (slot-value s1 'speed) 100)

  10. defmethod • Methods are defined separately from classes • Methods are specialized by the class they operate on

  11. Example (defmethod dive ((sub submarine) new-depth) (setf (slot-value sub 'depth) new-depth))

  12. Messaging Model of OOP • Most OO languages assume a message passing model • The method signature = message • Object with the method = recipient of message • But what about double-dispatch • transfer (account1, account2) • or even • equals (obj1, obj2) • We are forced to pick one object • implement method there

  13. Dispatch Model

  14. Dispatch Model of OOP • Methods independent of objects • Methods specialized for objects • but can be specialized for multiple objects

  15. Example • In polynomials domain • add can be multiply defined • add (term poly) • add (term term) • add (poly term) • add (poly poly)

  16. Slot specification • slot-value is awkward • can't be compiled • defclass has slot specifications • :accessor • :reader • :writer • :initform • :initarg • :allocation • :type

  17. initform • Default initial value • Can be overridden by arguments to make-instance • Evaluated each time make-instance is called

  18. initarg • A keyword that can be passed to make-instance • Gives slot its initial value • Overrides initform

  19. Example (defclass Submarine () ((Depth :initarg :depth :initform 100) (Speed :reader Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine :depth 200))

  20. Accessors • reader • Name of a function that will read the slot • writer • Name of a function that will set the slot's value • accessor • Name of a function to read and (with setf) write the slot's value

  21. Example 1 (defclass Submarine () ((Depth :reader Depth :initform 100) (Speed :reader Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (Depth Sub-1) ==> 100 (Speed Sub-1) ==> 5

  22. Example 2 (defclass Submarine () ((Depth :reader Depth :writer Set-Depth :initform 100) (Speed :reader Speed :writer Set-Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (Depth Sub-1) ==> 100 (Speed Sub-1) ==> 5 (Set-Depth 200 Sub-1) (Set-Speed 4 Sub-1) (Depth Sub-1) ==> 200 (Speed Sub-1) ==> 4

  23. Example 3 (defclass Submarine () ((Depth :accessor Depth :initform 100) (Speed :accessor Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (Depth Sub-1) ==> 100 (Speed Sub-1) ==> 5 (setf (Depth Sub-1) 200) (setf (Speed Sub-1) 4) (Depth Sub-1) ==> 200 (Speed Sub-1) ==> 4

  24. type • Specifies the type of the argument • Can be used for optimization

  25. allocation • Specifies instance or class allocation • Instance allocation means new copy of slot for each instance • instance variable in Java • Class allocation means one copy of instance for all instances • class variable in Java • declared with "static" (yuk!)

  26. Other Class Options • documentation • doc string associated with class • default-initargs • useful for overriding initforms from superclass

  27. defgeneric • Sort of like interface definition • Specify method name and argument list • Also • method-combination • argument-precedence-order

  28. Method combination • How to inherit methods? • normally evaluate most-specific method • polymorphism! • But there are other possibilities • Discounts • Supplier discount = 10% • Preferred supplier discount = an additional 2% • We might want to specify that discounts are combined rather than overridden • in Java, must call super.method directly

  29. In CLOS (defgeneric get-discount (firm) (:method-combination +)) (defclass supplier () ((discount :initform 10))) (defmethod get-discount + ((firm supplier)) (slot-value firm 'discount)) (defclass preferred-supplier (supplier) ()) (defmethod get-discount + ((firm preferred-supplier)) 2)

  30. Method Combination • Specify type of method combination in generic function • Each method is marked with its combination method • Methods are called from least to most-specific • Combination methods • progn, +, list, and, or, max, min, append and nconc

  31. Example • Polynomials • both terms and polynomials can be evaluated • We can change our code to create • term objects • polynomial objects

  32. Method combination • What about canonicalization? • we'd like to have new instances canonicalized when they are created • Can do this with an "after" method • write a method that is applied after initialize-instance does its thing

  33. Example

  34. Another example • iterators • infinite iterators • integer iterators • list iterators • accumulation iterators • mapping iterators

  35. Review • What have we covered in this class?

More Related