What is a type? Types in OO languages. What is a type? Types as sets. Example. Clients of screensaver

Similar documents
Recap. What is a type? Types in OO languages. What is a type? Types as sets. Up next, some advanced OO topics

Quiz. Programming Languages. CSE 130 : Fall Lecture 16: Static Types for Objects. Ranjit Jhala UC San Diego

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Object typing and subtypes

Subtyping. Lecture 13 CS 565 3/27/06

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Conformance. Object-Oriented Programming Spring 2015

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Lecture 13: Subtyping

Derived and abstract data types. TDT4205 Lecture 15

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS558 Programming Languages Winter 2013 Lecture 8

CSE341, Fall 2011, Lecture 26 Summary

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Exercise 8 Parametric polymorphism November 17, 2017

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Object Oriented Programming: Based on slides from Skrien Chapter 2

Types and Classes. I II From predefined (simple) and user-defined (composite) types via Abstract data types

Programming Languages and Techniques (CIS120)

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses:

Programming Languages and Techniques (CIS120)

Official Survey. Cunning Plan: Focus On Objects. The Need for a Calculus. Object Calculi Summary. Why Not Use λ-calculus for OO?

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Objects, Subclassing, Subtyping, and Inheritance

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

CS153: Compilers Lecture 11: Compiling Objects

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Name: CIS 341 Final Examination 10 December 2008

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

C++ Part 2 <: <: C++ Run-Time Representation. Smalltalk vs. C++ Dynamic Dispatch. Smalltalk vs. C++ Dynamic Dispatch

Part III Chapter 15: Subtyping

Subtyping (Dynamic Polymorphism)

C++ Yanyan SHEN. slide 1

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

G Programming Languages - Fall 2012

Part III. Chapter 15: Subtyping

CS 320 Introduction to Software Engineering Spring 2017

Parametric polymorphism and Generics

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

21 Subtyping and Parameterized Types

The Java Programming Language

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

System Analysis and Design. Introduction: Object Oriented Design

Konzepte von Programmiersprachen

Subtyping and Objects

Exercise 8 Parametric polymorphism November 18, 2016

Jam: A Smooth Extension With Java Mixins

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Subtyping (cont) Formalization of Subtyping. Lecture 15 CS 565. Inversion of the subtype relation:

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

CSE 331. Generics (Parametric Polymorphism)

SDV4001 Object Oriented Programming with C++ and UI Midterm 2013

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013

Some instance messages and methods

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

Outline. Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages

Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides)

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

Subtypes and Subclasses

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

Outline. More optimizations for our interpreter. Types for objects

Programming Languages and Techniques (CIS120)

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Subtyping (cont) Lecture 15 CS 565 4/3/08

CSCI Test 1.Spring 2004 Student Id: Grading: Undergrads, you are responsible for 110 points. Grads: you are responsible for 120 points

More Static Semantics. Static. One-Slide Summary. Lecture Outline. Typing Rules. Dispatch Rules SELF_TYPE

Subclassing, con.nued Method overriding, virtual methods, abstract classes/methods. COMP 401, Spring 2015 Lecture 9 2/19/2015

Variance. Svetlana Isakova

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

SOOL, a Simple Object-Oriented Language

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

History C++ Design Goals. How successful? Significant constraints. Overview of C++

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

CMSC 330: Organization of Programming Languages

Java Design Goals. Lecture 32: Java. Java Original implementations slow! Exceptions & Subtyping. - void method readfiles() throws IOException {...}!

Programming Languages and Techniques (CIS120)

301AA - Advanced Programming [AP-2017]

Data Abstraction. Hwansoo Han

Type Hierarchy. Lecture 6: OOP, autumn 2003

Functional Programming. Introduction To Cool

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CSE 331 Software Design & Implementation

Subtyping: broad definitions. Not Subtyping: type variable instantiation. Mutable Records (half like ML, half like Java/Scala)

Objects. say something to express one's disapproval of or disagreement with something.

Transcription:

What is a type? Types in OO languages What is a type? A description of an object: the fields it contains and the methods it contains that is to say: the attributes it contains (where for now we assume an attribute includes the type of the attribute) Types as sets One way to interpret types is as sets A type corresponds to the set of objects that are described by the type In other words, a description of the actions that one can successfully perform on an object Example The following type: interface Point { void screensaver(point [] p) { Corresponds to: set of objects having attributes getx, gety, move, jump, draw The object may have many other attributes! Compiler checks that screensaver only uses attributes of Point that are known to be there. Clients of screensaver So what objects can be passed in to screensaver? Intuitively, any object that has the given attributes should be ok. If the object has more attributes, no problem This intuitive notion is captured by the concept of subtyping 1

Subtyping A type T 1 is a subtype of type T 2, written T 1 <: T 2 if wherever an object of type T 2 can be used, so can an object of type T 1 Consequence: if T 1 <: T 2, then wherever an object of type T 2 is required, you can safely pass in an object of type T 1 Subtyping When can we say that T 1 <: T 2? When an object of type T 1 has all the attributes that an object of type T 2 has Subtyping with types-as-sets If: 1. T 1 <: T 2 2. objs(t 1 ) is the set of objects of type T 1 3. objs(t 2 ) is the set of objects of type T 2 then how are objs(t 1 ) and objs(t 2 ) related? Subtyping with types-as-sets If: 1. T 1 <: T 2 2. objs(t 1 ) is the set of objects of type T 1 3. objs(t 2 ) is the set of objects of type T 2 then how are objs(t 1 ) and objs(t 2 ) related? A: objs(t 1 ) objs(t 2 ) T 1 T 2 Example of subtyping Example of subtyping Recall: interface Point { interface ColorPoint { void setcolor(color c) Color getcolor() interface TextPoint { void settext(string s) string gettext() 2

Example of subtyping Another example objects Point void foo(triangle t) ColorPoint TextPoint Do we have ColorTriangle <: Triangle? Is it the case that wherever a Triangle is expected, it is safe to pass in a ColorTriangle? It it safe to pass ColorTriangle to foo? Another example Another example void foo(triangle t) void foo(triangle t) Safe to pass ColorTriangle to foo? Say foo only reads fields of t Then it s safe Safe to pass ColorTriangle to foo? Say foo only reads fields of t Then it s safe What if foo writes fields of t? Let s model this with methods Let s model this with methods void set_p1(point p1); void set_p1(colorpoint p1); t.set_p1(new Point(0,0)); Does anybody see the problem? void set_p1(point p1); void set_p1(colorpoint p1); t.set_p1(new Point(0,0)); Does anybody see the problem? If ColorTriangle is passed in to foo(), then ColorTriangle.set_p1() is called with a Point as an argument. Now what happens if ColorTriangle.set_p1() accesses p1.color? Runtime error! 3

So what s the rule then? So what s the rule then? void m(b c); void m(d d); void m(b c); void m(d d); We ve seen the answer is not D <: B Answer: B <: D Method params are contravariant subtyping relation between method parameters opposite of relation between classes Example revisited void set_p1(colorpoint p1); void set_p1(point p1); What about return values? B m(); D m(); t.set_p1(new Point(0,0)); // not allowed t.set_p1(new ColorPoint(0,0)); // no errors What about return values? B m(); D m(); Answer: D <: B Method return values are covariant subtyping relation in the same order as subtypting relation between the classes Let s go back to fields t.p1 := new Point() t := new ColorTriangle(); foo(t); t.p1.color // yikes! Read only fields are covariant Write-only field are contravariant Read/write fields invariant same type in subtype and supertype 4

What about Java Why does this not happen in Java? What about Java Why does this not happen in Java? Because Java has overloading: the name of a method includes its types, so all these methods in Java would actually be distinct. Arrays in Java Actually, that s not true! The covariance, contravariance issues does affect Java too! Array subtyping: Point [] and ColoredPoint [] Arrays in Java Actually, that s not true! The covariance, contravariance issues does affect Java too! Array subtyping: Point [] and ColoredPoint [] interface Point [] { void set(int idx, Point val); Point get(int idx); interface ColoredPoint [] { void set(int idx, ColoredPoint val); ColoredPoint get(int idx); interface Point [] { void set(int idx, Point val); Point get(int idx); interface ColoredPoint[] { void set(int idx, ColoredPoint val); ColoredPoint get(int idx); if ColoredPoint [] <: Point [] then: ColoredPoint [] x = new ColoredPoint [10]; Point [] y = x; y.set(0, new Point(10,10)); // Yikes! if ColoredPoint [] <: Point [] then: ColoredPoint [] x = new ColoredPoint [10]; Point [] y = x; y.set(0, new Point(10,10)); // Yikes! Conclusion: it is not the case that ColoredPoint[] <: Point [] But Recap Java does consider ColoredPoint [] to be a subtype of Point[]. What gives? Runtime checks are inserted to detect errors at runtime! ColoredPoint [] x = new ColoredPoint[10]; Point [] y = x; y[0] := new Point(0,0); ColoredPoint z = x[0]; z.color; // yikes! runtime exception! So we ve seen a definition of when one interface subtypes another. This definition depends on looking at the attributes of the interfaces, and figuring out if they match. 5

But in Java So we ve seen a definition of when one interface subtypes another. This definition depends on looking at the attributes of the interfaces, and figuring out if they match. But how does one really figure out in Java if an interface subtypes another? But in Java So we ve seen a definition of when one interface subtypes another. This definition depends on looking at the attributes of the interfaces, and figuring out if they match. But how does one really figure out in Java if an interface subtypes another? Look at the subclass relationship! Structural vs. nominal subtyping What we ve seen until now: structural subtyping the subtyping relation is determined based on the structure of the types (ie: the attributes in the interfaces) similar to duck typing in Python (although Python is dynamic) Nominal subtyping (by name subtyping): the subtyping relation is given by the programmer Next: multimethods! Go to code 6