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

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

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

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

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Conformance. Object-Oriented Programming Spring 2015

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

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

Lecture 13: Subtyping

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE341, Fall 2011, Lecture 26 Summary

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CS153: Compilers Lecture 11: Compiling Objects

Exercise 8 Parametric polymorphism November 17, 2017

Programming Languages and Techniques (CIS120)

Derived and abstract data types. TDT4205 Lecture 15

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

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

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

CS 320 Introduction to Software Engineering Spring 2017

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

CS558 Programming Languages Winter 2013 Lecture 8

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

Programming Languages and Techniques (CIS120)

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

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

Objects, Subclassing, Subtyping, and Inheritance

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

Parametric polymorphism and Generics

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

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.

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

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

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

Part III Chapter 15: Subtyping

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

Part III. Chapter 15: Subtyping

Subtyping (Dynamic Polymorphism)

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

G Programming Languages - Fall 2012

C++ Yanyan SHEN. slide 1

Jam: A Smooth Extension With Java Mixins

System Analysis and Design. Introduction: Object Oriented Design

Some instance messages and methods

21 Subtyping and Parameterized Types

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

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

The Java Programming Language

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

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

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

Subtyping and Objects

Konzepte von Programmiersprachen

Exercise 8 Parametric polymorphism November 18, 2016

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

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

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

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

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

Programming Languages and Techniques (CIS120)

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

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)

CS263: Runtime Systems Lecture: High-level language virtual machines

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

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

Programming Languages and Techniques (CIS120)

Subtypes and Subclasses

CS422 - Programming Language Design

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

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

Outline. More optimizations for our interpreter. Types for objects

CSE wi Final Exam 3/12/18. Name UW ID#

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

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

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

Overview. Elements of Programming Languages. Records. Named variants. Last time: Simple data structures: pairing (product types), choice (sum types)

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Variance. Svetlana Isakova

SOOL, a Simple Object-Oriented Language

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

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

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

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

Type Hierarchy. Lecture 6: OOP, autumn 2003

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

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

CSE wi Final Exam 3/12/18 Sample Solution

301AA - Advanced Programming [AP-2017]

Transcription:

Recap Recap from last time And continuation of decorators See code Up next, some advanced OO topics Today: Typing of object oriented languages Next time: Multi-methods 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 1

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 Subtyping 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 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: obs(t 1 ) objs(t 2 ) T 1 T 2 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() 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? 3

Let s model this with methods Let s model this with methods void set_p1(point p1); void set_p1(colorpoint p1); void foo(triangle t) { t.set_p1(new Point(0,0)); Does anybody see the problem? void set_p1(point p1); void set_p1(colorpoint p1); void foo(triangle t) { 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! 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 What about return values? What about return values? B m(); D m(); B m(); D m(); Answer: D <: B Method return values are covariant subtyping relation in the same order as subtypting relation between the classes 4

Let s go back to fields void foo(triangle t) { t.p1 := new Point() t := new ColorTriagle(); foo(t); t.p1.color // yikes! What about Java Why does this not happen in Java? Read only fields are covariant Write-only field are contravariant Read/write fields invariant same type in subtype and supertype 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 [] interface Point [] { void set(int idx, Point val); Point get(int idx); if ColoredPoint [] <: Point [] then: interface ColoredPoint[] { void set(int idx, ColoredPoint val); ColoredPoint get(int idx); ColoredPoint [] x = new ColoredPoint [10]; Point [] y = x; y.set(0, new Point(10,10)); // Yikes! 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); if ColoredPoint [] <: Point [] then: ColoredPoint [] x = new ColoredPoint [10]; Point [] y = x; y.set(0, new Point(10,10)); // Yikes! interface ColoredPoint[] { void set(int idx, ColoredPoint val); ColoredPoint get(int idx); Conclusion: it is not the case that ColoredPoint[] <: Point [] But 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! 5

Recap 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 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 time: multimethods! 6