binary methods programming
play

Binary Methods Programming: Non-issues the C LOS Perspective Types, - PowerPoint PPT Presentation

C LOS Binary Methods Didier Verna Introduction Binary Methods Programming: Non-issues the C LOS Perspective Types, Classes, Inheritance Method comb. Usage Introspection Binary function class Didier Verna Implementation


  1. C LOS Binary Methods Didier Verna Introduction Binary Methods Programming: Non-issues the C LOS Perspective Types, Classes, Inheritance Method comb. Usage Introspection Binary function class Didier Verna Implementation Misimplementations Bin Completeness didier@lrde.epita.fr Conclusion http://www.lrde.epita.fr/˜didier May 23 – ELS 2008 1/36

  2. Introduction What are binary methods? C LOS Binary Binary Operation: 2 arguments of the same type Methods Didier Verna Examples: arithmetic / ordering relations ( = , + ,> etc. ) OO Programming: 2 objects of the same class Introduction Non-issues Benefit from polymorphism etc. Types, Classes, Inheritance ⇒ Hence the term binary method Method comb. Usage However: [Bruce et al., 1995] Introspection ◮ problematic concept in traditional OO languages Binary function class ◮ type / class relationship in the context of inheritance Implementation Misimplementations Bin Completeness Conclusion 2/36

  3. Table of contents C LOS Binary Methods Didier Verna Binary Methods non-issues 1 Types, Classes, Inheritance Introduction Corollary: method combinations Non-issues Types, Classes, Inheritance Method comb. Enforcing the concept – usage level Usage 2 Introspection Introspection Binary function class Implementation Binary function class Misimplementations Bin Completeness Conclusion Enforcing the concept – implementation level 3 Misimplementations Binary Completeness 3/36

  4. Types, Classes, Inheritance The context C LOS Binary The Point class hierarchy Methods Didier Verna Point Introduction x, y : Integer Non-issues Types, Classes, Inheritance Method comb. equal (Point) : Boolean Usage Introspection Binary function class Implementation Misimplementations Bin Completeness Conclusion ColorPoint color : String equal (ColorPoint) : Boolean 5/36

  5. C++ implementation attempt #1 Details omitted C LOS Binary The C++ Point class hierarchy Methods Didier Verna class Point { Introduction int x , y ; Non-issues Types, Classes, Inheritance bool equal ( Point& p ) Method comb. { return x == p . x && y == p . y ; } Usage } ; Introspection Binary function class class ColorPoint : public Point Implementation { Misimplementations std : : s t r i n g color ; Bin Completeness Conclusion bool equal ( ColorPoint& cp ) { return color == cp . color && Point : : equal ( cp ) ; } } ; 6/36

  6. But this doesn’t work. . . Overloading is not what we want C LOS Binary Looking through base class references Methods Didier Verna int main ( int argc , char ∗ argv [ ] ) { Introduction Point & p1 = ∗ new ColorPoint (1 , 2 , " red " ) ; Non-issues Point & p2 = ∗ new ColorPoint (1 , 2 , " green " ) ; Types, Classes, Inheritance Method comb. std : : cout << p1 . equal ( p2 ) << std : : endl ; Usage / / => True . #### Wrong ! Introspection } Binary function class Implementation Misimplementations ColorPoint::equal only overloads Point::equal Bin Completeness Conclusion From the base class, only Point::equal is seen We want the definition from the exact class 7/36

  7. C++ implementation attempt #2 Details still omitted C LOS Binary The C++ Point class hierarchy Methods Didier Verna class Point { Introduction int x , y ; Non-issues Types, Classes, Inheritance v i rt ua l bool equal ( Point& p ) Method comb. { return x == p . x && y == p . y ; } Usage } ; Introspection Binary function class class ColorPoint : public Point Implementation { Misimplementations std : : s t r i n g color ; Bin Completeness Conclusion v i rt ua l bool equal ( ColorPoint& cp ) { return color == cp . color && Point : : equal ( cp ) ; } } ; 8/36

  8. But this doesn’t work either. . . Still got overloading, still not what we want C LOS Binary The forbidden fruit Methods Didier Verna v i rtua l bool equal ( Point & p ) ; v i rtua l bool equal ( ColorPoint & cp ) ; / / #### Forbidden ! Introduction Non-issues Types, Classes, Invariance required on virtual methods argument types Inheritance Method comb. Worse: virtual keyword silently ignored Usage Introspection (overloading behavior, just as before) Binary function class Implementation Why? To preserve static type safety Misimplementations Bin Completeness Conclusion 9/36

  9. Why the typing would be unsafe And lead to errors at run-time C LOS Binary Example of run-time typing error Methods Didier Verna In fact, a ColorPoint Just a Point Introduction Non-issues Types, Classes, Inheritance bool foo (Point& p1, Point& p2) Method comb. Usage { Introspection return p1.equal (p2); Binary function class Implementation } Misimplementations Bin Completeness Conclusion The ColorPoint implementation But gets only a Point ! expects a ColorPoint argument (ex. accesses the color field) 10/36

  10. Constraints for type safety When subtyping a polymorphic method C LOS Binary The covariance / contravariance rule Methods ◮ supertype the arguments ( contravariance ) Didier Verna ◮ subtype the return value ( covariance ) Introduction Note: C++ is even more constrained Non-issues Types, Classes, ◮ The argument types must be invariant Inheritance Method comb. Note: Eiffel allows for arguments covariance Usage ◮ But this leads to possible run-time errors Introspection Binary function class Analysis: [Castagna, 1995]. Implementation Misimplementations ◮ Lack of expressiveness Bin Completeness subtyping (by subclassing) � = specialization Conclusion ◮ Object model defect single dispatch (not the record-based model) 11/36

  11. C LOS : the Common Lisp Object System A different model C LOS Binary Class methods vs. Generic functions Methods ◮ C++ methods belong to classes Didier Verna ◮ C LOS generic functions look like ordinary functions Introduction (outside classes) Non-issues Single dispatch vs. Multi-methods Types, Classes, Inheritance Method comb. ◮ C++ dispatch: the first (hidden) argument’s type ( this ) Usage ◮ C LOS dispatch: the type of any number of arguments Introspection Binary function class Implementation Misimplementations Bin Completeness Conclusion 12/36

  12. C LOS implementation No detail omitted C LOS Binary The C LOS Point class hierarchy Methods Didier Verna ( defclass point ( ) ( ( x : i n i t a r g : x : reader point − x ) Introduction ( y : i n i t a r g : y : reader point − y ) ) ) Non-issues Types, Classes, Inheritance ( defclass color − point ( point ) Method comb. ( ( color : i n i t a r g : color : reader point − color ) ) ) Usage Introspection ; ; optional Binary function class ( defgeneric point= ( a b ) ) Implementation Misimplementations ( defmethod point= ( ( a point ) ( b point ) ) Bin Completeness ( and (= ( point − x a ) ( point − x b ) ) Conclusion (= ( point − y a ) ( point − y b ) ) ) ) ( defmethod point= ( ( a color − point ) ( b color − point ) ) ( and ( string = ( point − color a ) ( point − color b ) ) ( call − next − method ) ) ) 13/36

  13. How to use it? Just like ordinary function calls C LOS Binary Using the generic function Methods Didier Verna ( l e t ( ( p1 ( make − point : x 1 : y 2)) ( p2 ( make − point : x 1 : y 2)) Introduction ( cp1 ( make − color − point : x 1 : y 2 : color " red " ) ) Non-issues ( cp2 ( make − color − point : x 1 : y 2 : color " green " ) ) ) Types, Classes, Inheritance ( values ( point= p1 p2 ) Method comb. ( point= cp1 cp2 ) ) ) Usage ; ; => (T NIL ) Introspection Binary function class Implementation Method selection: both arguments Misimplementations Bin Completeness (multiple dispatch) Conclusion Function call syntax: more pleasant aesthetically ( p1.equal(p2) or p2.equal(p1) ?) ⇒ Hence the term binary function 14/36

  14. Applicable methods There are more than one. . . C LOS Binary To avoid code duplication: Methods ◮ C++: Point::equal() Didier Verna ◮ C LOS : (call-next-method) Introduction Applicable methods: Non-issues Types, Classes, ◮ All methods compatible with the arguments classes Inheritance Method comb. ◮ Sorted by (decreasing) specificity order Usage ◮ call-next-method calls the next most specific Introspection applicable method Binary function class Implementation Method combinations: Misimplementations Bin Completeness ◮ Ways of calling several (all) applicable methods Conclusion (not just the most specific one) ◮ Predefined method combinations: and , or , progn etc. ◮ User definable 15/36

  15. Using the and method combination Comes in handy for the equality concept C LOS Binary The and method combination Methods Didier Verna ( defgeneric point= ( a b ) ( : method − combination and ) Introduction ) Non-issues Types, Classes, ( defmethod point= and ( ( a point ) ( b point ) ) Inheritance Method comb. ( and (= ( point − x a ) ( point − x b ) ) Usage (= ( point − y a ) ( point − y b ) ) ) ) Introspection Binary function class ( defmethod point= and ( ( a color − point ) ( b color − point ) ) Implementation ( and ( call − next − method ) Misimplementations ( string = ( point − color a ) ( point − color b ) ) Bin Completeness ) Conclusion ) In C LOS , the generic dispatch is (re-)programmable 19/36

Recommend


More recommend