Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multi-Methods in Cecil

Similar presentations


Presentation on theme: "Multi-Methods in Cecil"— Presentation transcript:

1 Multi-Methods in Cecil
Objects and Aspects (Fall 2004) September 20, 2004 Kevin Bierhoff

2 Objects and Aspects: Multi-Methods in Cecil
Agenda Generic functions Cecil Object model Method dispatch Encapsulation Open questions Discussion any time September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

3 Generic functions allow dynamic dispatch on multiple arguments
Define the generic function add(a, b) with add(Integer a, Integer b) add(Integer a, Double b) add(Double a, Integer b) add(Double a, Double b) Which method receives add(x, y) ? Depends on runtime type of x and y Java’s x.add(y) would only look at x! September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

4 Generic functions can be simulated in object-oriented languages …
class Object boolean equals(Object other) return other.equalsOther(this) boolean equalsOriginal(Object original) return this == original class Point extends Object protected int x, y; boolean equalsOriginal(Point original) return x == original.x && y == original.y; “Double Dispatch” September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

5 Behavior for new classes cannot be added later
… but it’s a real pain class Point [cont.] boolean equals(Object other) return other.equalsOther(this) class ShadowPoint extends Point boolean equalsOriginal(Point original) return x == (original.x – 5) && y == (original.y – 5) boolean equalsOriginal(ShadowPoint original) return x == original.x && y == original.y return x == (original.x + 5) && y == (original.y + 5) Static binding Consistent behavior Code both sides Behavior for new classes cannot be added later September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

6 Generic functions as in CLOS foster functional programming
Generic functions are external to objects Leave only data fields within the objects Implicit generic functions for accessor methods Prohibit data encapsulation Incredibly complex inheritance rules To resolve dispatch ambiguities (cf.) Really not transparent to programmer Objects are really structs Cecil addresses both problems mentioned here September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

7 Cecil builds its object model on Self
Object model based on prototypes Supports multiple inheritance Root object called “any” Adds or modifies several ideas Inheritance vs. subtyping of objects Abstract, template, and unique objects Local vs. shared fields and their accessors We will look at each of these 3 points in turn September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

8 Cecil distinguishes inheritance and subtyping of objects
Adopt behavior through inheritance int = object inherits number Override methods to adapt behavior Get new concrete object at runtime var ::= object inherits int Adopt interface through subtyping type collection type list subtypes collection set = object inherits array subtypes collection We will focus on inheritance Inheritance crucial for method dispatch September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

9 Cecil can enforce special roles of objects at runtime
abstract objects are not completely defined Can contain abstract methods Similar to abstract classes in Java template objects for concrete incarnations Cannot be manipulated at runtime int = template object inherits number var ::= object inherits int int.set_value(5) -- is invalid unique objects exist exactly once zero = unique object inherits number var ::= object inherits zero -- is invalid Unique be inherited neither at compile time nor at runtime September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

10 Objects and Aspects: Multi-Methods in Cecil
Objects can have state Define data fields for objects { field } By default fields are local to an object Can also be shared between objects Each field implicitly defines pair of accessors Get with value(num), set it with set_value(num, 5) Fields can be declared read-only / init-only var ::= object inherits int [value := 5] -- for initialization Note: Self shares fields on inheritance Thus usually need a “traits” object with behavior and an inheriting “template” that holds fields and is cloned This separation is not necessary in Cecil Shared fields are like class variables in java Difference between read-only and init-only? September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

11 Cecil associates multi-methods with all objects involved
Constrained arguments for multi-methods Dynamic dispatch on all constrained arguments OOP as special case No constrained argument  static function One constrained argument  ~ OOP (Single-Dispatch) Two or more constrained arguments  Multi-Dispatch Multi-methods belong to all constrained args Code is now best viewed as a graph Why only ~OOP? int + int int + double int double double + int double + double September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

12 Constrain arguments with @
Template objects int = template object inherits number double = template object inherits number Methods + { … } + { … } + { … } + { … } September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

13 Multi-methods provoke ambiguities similar to multiple inheritance …
“Diamond” relationships collection elem) unsorted_list sorted_list elem) elem) set Now what happens if you call “contains” with a set? September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

14 … as well as new ambiguities related to multiple dispatch
collection sorted_list Now what happens if you call “merge” with two sorted_list objects? September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

15 Cecil leaves these ambiguities to the programmer
Cecil’s dispatch mechanism Find syntactically fitting methods Ignore those with a too specialized argument Find the most specialized under the remaining Ambiguity  runtime exception Programmer must resolve manually Note: CLOS avoids ambiguities By totally ordering all methods September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

16 Cecil allows information hiding …
Fields and methods can be private Really means protected Access based on caller privileges Analyze signature of calling method Match calling to requested objects Grant access to all super- and sub-objects { if(has_changed(s), { draw_shape(s) }, { } ) } { draw_shape(v) visible Protected should mean what it means in oo: Not a single method is protected, but all methods of the same name In other words, you should access everything down the hierarchy that you have yourself That what chambers proposes at the end of chapter 3 (p. 19) What is the difference to OO? shape private x y size_x size_y rectange private September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

17 … but tolerates serious encapsulation breaches
Want to break into an object? Just define a new method that takes this object as a constrained argument { access private fields now } { access whatever you want } Pro: Easy extensibility of class hierarchy Is that good or evil? September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

18 Cecil solves issues in CLOS …
Generic functions External to object hierarchy Foster functional programming approach Fails to address object encapsulation Cecil Multi-methods Belong to all dynamic arguments Graph-based object-oriented programming Offers (some) encapsulation September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

19 … but raises new questions
Do you like the object model better than Self’s? how Cecil (does not re)solve ambiguities? that methods belong to multiple objects? Hey, where is the code hierarchy? Is this really better than double dispatch? Does this really feel like (multi-) object-oriented programming? Object model: Inheritance vs. subtyping of objects Abstract, template, and unique objects Local vs. shared fields and their accessors Better than double dispatch: graph property of code makes maintenance hard Even if we had protected methods as in OO – is it OO? September 20, 2004 Objects and Aspects: Multi-Methods in Cecil


Download ppt "Multi-Methods in Cecil"

Similar presentations


Ads by Google