What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Sheeple: Dynamic Object-Orientation for CL Josh Marchán 22 October 2009 Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple 1 What is Class-orientation? 2 What is Object-oriented Programming? 3 CLOS 4 Sheeple Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Setting up the strawman. . . Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Setting up the strawman. . . Write general blueprints, create objects based on them to hold some (limited) changeable state. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Setting up the strawman. . . Write general blueprints, create objects based on them to hold some (limited) changeable state. Otherwise, objects are fairly static Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Setting up the strawman. . . Write general blueprints, create objects based on them to hold some (limited) changeable state. Otherwise, objects are fairly static Nothing object-oriented about classes. Saying class-orientation is object-oriented is a damn lthy lie. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Setting up the strawman. . . Write general blueprints, create objects based on them to hold some (limited) changeable state. Otherwise, objects are fairly static Nothing object-oriented about classes. Saying class-orientation is object-oriented is a damn lthy lie. "Static constructs considered harmful" (for dynamic languages) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Hope? Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Hope? Python - arbitrary attributes Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Hope? Python - arbitrary attributes Ruby - open classes Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Hope? Python - arbitrary attributes Ruby - open classes Smalltalk - Everything is an instance of a class, including classes. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Hope? Python - arbitrary attributes Ruby - open classes Smalltalk - Everything is an instance of a class, including classes. CLOS - class redenition, change-class, EQL specializers, MOP Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Developing a game Goal: Write a game You're mapping to conceptual objects. Class-orientation seems like a perfect t. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Developing a game Goal: Write a game You're mapping to conceptual objects. Class-orientation seems like a perfect t. Or is it? Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: The class hierarchy Let's write a class hierarchy! Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: The class hierarchy Let's write a class hierarchy! Class GameObject (no instances) \_ Class Weapon (no instances) \_ Class Sword (no instances) \_ Class FireSword (maybe an instance?) \_ ??? (What now?) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: The class hierarchy Let's write a class hierarchy! Class GameObject (no instances) \_ Class Weapon (no instances) \_ Class Sword (no instances) \_ Class FireSword (maybe an instance?) \_ ??? (What now?) Suggestions? Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Working toward a solution Some solutions: Class explosion Programmatic class creation Data-driven programming Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Solutions Class explosion: Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Solutions Class explosion: Still class-oriented. Lots of manual class writing Might make code. . . dicult Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Solutions Class explosion: Still class-oriented. Lots of manual class writing Might make code. . . dicult Do you really want to write FireIceMagicMissileGoldenGodForeverSwordOfDoom? Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Solutions Programmatic class creation: Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Solutions Programmatic class creation: May not actually be possible in your language Programmatic classes are sketchy, at best. Using them may be dicult and arcane. Reusability possibly out the window. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Solutions Data-driven programming Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Solutions Data-driven programming Eject! Eject! Class-orientation can't actually do what you want, so you need to use something else Blessing You've escaped the hell that is class-based programming Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Screw this. From a paper on Self "How hardcore do you want to be? How many lifetimes do you want to waste?" Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple What is Object-Oriented Programming? Object-oriented programming: A dierent paradigm for putting together your program. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Overview of objects Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Overview of objects Objects are like other objects, and share each others' behavior. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Overview of objects Objects are like other objects, and share each others' behavior. No separate entity to confuse the process. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Overview of objects Objects are like other objects, and share each others' behavior. No separate entity to confuse the process. Delegate behavior and data. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Structuring an application with objects Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Structuring an application with objects Create an object Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Structuring an application with objects Create an object Copy or delegate to that object Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Structuring an application with objects Create an object Copy or delegate to that object New object can act as a prototype for other objects Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Structuring an application with objects Create an object Copy or delegate to that object New object can act as a prototype for other objects Don't Panic! Lots of other object-oriented design principles you learned still apply, in a good way. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Game objects revisited Back to the game. Now with objects: Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Game objects revisited Back to the game. Now with objects: Object \_ Object, add some Weapon-like attributes \_ Weapon, add some Sword-like attributes \_ Sword, add some Fire-like attributes \_ ???, add some ???-like attributes Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: Game objects revisited Back to the game. Now with objects: Object \_ Object, add some Weapon-like attributes \_ Weapon, add some Sword-like attributes \_ Sword, add some Fire-like attributes \_ ???, add some ???-like attributes Awesome! Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example: That's totally not dierent at all! D:< What's the dierence? Make objects dierent by making them dierent Incremental development on a live system Objects dened by what they do, and what attributes they have, not by an abstract blueprint Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example (cont'd) What does this mean? Create a new object that implements the new behavior. Delegate what you want. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example (cont'd) What does this mean? Create a new object that implements the new behavior. Delegate what you want. Programmatic creation of objects is easy and natural (not like there's any other way to do it!) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example (cont'd) What does this mean? Create a new object that implements the new behavior. Delegate what you want. Programmatic creation of objects is easy and natural (not like there's any other way to do it!) Heck, make programmatic object creation part of the design tool Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Example (cont'd) What does this mean? Create a new object that implements the new behavior. Delegate what you want. Programmatic creation of objects is easy and natural (not like there's any other way to do it!) Heck, make programmatic object creation part of the design tool Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Object-orientation in the wild Prototype and Properties "patterns" (for classes) JavaScript MUDs LambdaMOO LPC DGD Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple CLOS Common Lisp Object System. Integrates with Common Lisp Multiple inheritance Multiple dispatch Method combination Metaobject Protocol Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple More dynamic than other class-based systems Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple More dynamic than other class-based systems Redenition of classes with instance updates (Very complicated, though. See CLHS 4.3.6) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple More dynamic than other class-based systems Redenition of classes with instance updates (Very complicated, though. See CLHS 4.3.6) Adding/removing generic functions and methods. Does not require redenition of a class. Generic functions and classes conceptually separate. Very dynamic Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple More dynamic than other class-based systems Redenition of classes with instance updates (Very complicated, though. See CLHS 4.3.6) Adding/removing generic functions and methods. Does not require redenition of a class. Generic functions and classes conceptually separate. Very dynamic Instances can change classes (Again, pretty tricky. CLHS 7.2) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple WANT I want: Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple WANT I want: something with CLOS' nice features, but object-oriented Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple WANT I want: something with CLOS' nice features, but object-oriented something portable, built on Common Lisp Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple WANT I want: something with CLOS' nice features, but object-oriented something portable, built on Common Lisp an API similar to CLOS. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple WANT I want: something with CLOS' nice features, but object-oriented something portable, built on Common Lisp an API similar to CLOS. A pony. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Sheeple Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Sheeple Sheeple: Dynamic Object-Orientation for Common Lisp. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Inuences Written by Josh Marchán and Adlai Chandrasekhar Inuenced by: CLOS Slate: http://slatelanguage.org Self: http://research.sun.com/self Io: http://iolanguage.com LambdaMOO and similar MUD systems Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Objects, with delicious CLOS Lisp is dynamic. The object system should dynamic. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Objects, with delicious CLOS Lisp is dynamic. The object system should dynamic. CLOS+Object-orientation. What more could you possibly want? Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Objects, with delicious CLOS Lisp is dynamic. The object system should dynamic. CLOS+Object-orientation. What more could you possibly want? (object :parents *pony*) => Here's your pony. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Looks suspiciouly like CLOS on the surface Multiple inheritance Multiple dispatch Method combination Lisp integration (autoboxing) Interface looks like a M-% of CLOS' API Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple But has all the proto-goodies you might want underneath. . . Objects dene behavior Completely dynamic Dynamic delegation of data (not just behavior) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple . . . and it still performs well Self showed that prototype languages can be ecient. Self-inspired maps ("hidden classes") Fast property access, reuse of known techniques from Class-land Delegation can lead to smaller memory footprint NewtonScript exploited this for an embedded system. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple It's fun. Being able to manipulate application on the y makes programming more fun and productive! Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple It's fun. Being able to manipulate application on the y makes programming more fun and productive! This T-Rex just downloaded Sheeple. Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Object API Object creation Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Object API Object creation Make a new object (object) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Object API Object creation Make a new object (object) Make a new object, that delegates to another object (object :parents *foo*) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Object API Object creation Make a new object (object) Make a new object, that delegates to another object (object :parents *foo*) Copy an existing object, directly inheriting its properties locally (clone *foo*) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Property API Creating and managing properties Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Property API Creating and managing properties Add a property (add-property *foo* 'name "value") Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Property API Creating and managing properties Add a property (add-property *foo* 'name "value") Access a property (property-value *foo* 'name) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Property API Creating and managing properties Add a property (add-property *foo* 'name "value") Access a property (property-value *foo* 'name) Access a delegated property from an object's descendant (property-value (object :parents *foo*) 'name) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Property API Creating and managing properties Add a property (add-property *foo* 'name "value") Access a property (property-value *foo* 'name) Access a delegated property from an object's descendant (property-value (object :parents *foo*) 'name) Remove the property (remove-property *foo* 'name) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Message API Creating messages, replies, and dispatching them Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Message API Creating messages, replies, and dispatching them Create a message (think generic functions) (defmessage synergize (a b) (:documentation "Synergizes A and B")) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Message API Creating messages, replies, and dispatching them Create a message (think generic functions) (defmessage synergize (a b) (:documentation "Synergizes A and B")) Create a reply (think methods) (defreply synergize ((a =string=) (b =string=)) (concatenate 'string a b)) Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Message API Creating messages, replies, and dispatching them Create a message (think generic functions) (defmessage synergize (a b) (:documentation "Synergizes A and B")) Create a reply (think methods) (defreply synergize ((a =string=) (b =string=)) (concatenate 'string a b)) Call a message (think uhh. . . calling a function :D) (synergize "foo" "bar") => "foobar" Josh Marchán Sheeple: Dynamic Object-Orientation for CL What is Class-orientation? What is Object-oriented Programming? CLOS Sheeple Links: Project page on my site: http://sykosomatic.org/sheeple Project page on Github: http://github.com/sykopomp/sheeple Josh Marchán Sheeple: Dynamic Object-Orientation for CL