Multi-Methods in Cecil

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
1 OBJECT-ORIENTED CONCEPTS. 2 What is an object?  An object is a software entity that mirrors the real world in some way.  A software object in OOP.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Road Map Introduction to object oriented programming. Classes
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Classes, Interfaces and Packages
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Object-Oriented Programming Concepts
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Software Engineering Fall 2005
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Advanced Programming in Java
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Java Programming Language
Advanced Programming Behnam Hatami Fall 2017.
More Object-Oriented Programming
Object Oriented Programming
Announcements & Review
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Advanced Programming in Java
Review of Previous Lesson
Data Structures and ADTs
Agenda Software development (SD) & Software development methodologies (SDM) Orthogonal views of the software OOSD Methodology Why an Object Orientation?
Inheritance Lakshmish Ramaswamy.
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

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

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

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

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

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

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

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

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

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

Objects and Aspects: Multi-Methods in Cecil Objects can have state Define data fields for objects value(n@number) { 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

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

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

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

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

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

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 draw(s@shape) { if(has_changed(s), { draw_shape(s) }, { } ) } draw(v@visible) { draw_shape(v) visible draw(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? draw(s@shape) shape private draw_shape(s@shape) x y size_x size_y rectange private draw_shape(s@shape) September 20, 2004 Objects and Aspects: Multi-Methods in Cecil

… but tolerates serious encapsulation breaches Want to break into an object? Just define a new method that takes this object as a constrained argument breach(a@rectangle) { access private fields now } breach(a@any) { 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

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

… 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