Late Binding; OOP as a Racket Pattern

Similar documents
Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

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

CSE341, Fall 2011, Lecture 26 Summary

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods. Dan Grossman Autumn 2018

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

CS 360 Programming Languages Interpreters

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

CSE 341: Programming Languages

CSE 341, Autumn 2015, Ruby Introduction Summary

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

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

VIRTUAL FUNCTIONS Chapter 10

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Semantic Analysis. Lecture 9. February 7, 2018

CSE341: Programming Languages Lecture 20 Arrays and Such, Blocks and Procs, Inheritance and Overriding. Dan Grossman Spring 2017

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Data Abstraction. Hwansoo Han

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

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

CSE 401/M501 Compilers

Functions. Nate Foster Spring 2018

CS558 Programming Languages Winter 2013 Lecture 8

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

Overview. Elements of Programming Languages. Objects. Self-Reference

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

CSE341, Spring 2013, Final Examination June 13, 2013

The role of semantic analysis in a compiler

Semantic Analysis and Type Checking

301AA - Advanced Programming [AP-2017]

Inheritance and object compatibility

Lecture Notes on Programming Languages

Operational Semantics. One-Slide Summary. Lecture Outline

CSE 504: Compiler Design. Intermediate Representations Symbol Table

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

Project 5 Due 11:59:59pm Tuesday, April 25, 2017

The Environment Model. Nate Foster Spring 2018

PROGRAMMING LANGUAGE 2

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Principles of Programming Languages. Lecture Outline

Object Oriented Issues in VDM++

What are the characteristics of Object Oriented programming language?

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

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

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Data Structures (list, dictionary, tuples, sets, strings)

CSE341: Programming Languages Lecture 22 OOP vs. Functional Decomposition; Adding Operators & Variants; Double-Dispatch. Dan Grossman Autumn 2018

Interface evolution via public defender methods

The Environment Model

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CMSC330 Fall 2013 Practice Problems 6 Solutions

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Ways to implement a language

Exceptions and Continuations. Lecture #19: More Special Effects Exceptions and OOP. Approach II: Non-Standard Return. Approach I: Do Nothing

Object Oriented Programming. Java-Lecture 11 Polymorphism

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Overview. Elements of Programming Languages. Objects. Self-Reference

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

CS558 Programming Languages

Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

A Short Summary of Javali

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Objects. Prof. Clarkson Fall ob-ject: to feel distaste for something Webster's Dictionary. Today s music: Kung Fu Fighting by CeeLo Green

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

Rationale for TR Extension to the programming language C. Decimal Floating-Point Arithmetic

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

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

[ L5P1] Object-Oriented Programming: Advanced Concepts

CS-202 Introduction to Object Oriented Programming

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

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Project 5 Due 11:59:59pm Tue, Dec 11, 2012

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Chapter 10 :: Data Abstraction and Object Orientation

Chapter 3:: Names, Scopes, and Bindings (cont.)

CS 360 Programming Languages Day 14 Closure Idioms

Chapter 3:: Names, Scopes, and Bindings (cont.)

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Compilers. Cool Semantics II. Alex Aiken

Inheritance. Transitivity

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

QUESTIONS FOR AVERAGE BLOOMERS

CS 251 Intermediate Programming Inheritance

Classes and Objects. Object Orientated Analysis and Design. Benjamin Kenwright

Inheritance and Interfaces

Another IS-A Relationship

AP CS Unit 6: Inheritance Notes

Transcription:

Late Binding; OOP as a Racket Pattern

Today Dynamic dispatch aka late binding aka virtual method calls Call to self.m2() in method m1 defined in class C can resolve to a method m2 defined in a subclass of C Most unique characteristic of OOP Need to define the semantics of objects and method lookup as carefully as we defined variable lookup for functional programming Then consider advantages, disadvantages of dynamic dispatch Then encoding OOP / dynamic dispatch with pairs and functions In Racket Complement to learning lambdas/closures in C++

Resolving identifiers The rules for "looking up" various symbols in a PL is a key part of the language's definition So discuss in general before considering dynamic dispatch Racket: Look up variables in the appropriate environment Key point of closures' lexical scope is defining "appropriate" Java: Lexical scoping like Racket But also have instance variables, class variables, and methods! All of these combined with inheritance "break" lexical scope!

class A { int x; int method() { } }! class B extends A { int method() { } }! in some main method somewhere: B b = new B();! b.x = 17;! // lookup x where?! // - in current environment -> not found! // - in "environment for class B" -> not found! // - rule to look in "enclosing environment"! // doesn't help here either.!

class A { int x; int method() { } }! class B extends A { int method() { } }! in some main method somewhere: A waitwhat;! if (Math.random() >.5)! waitwhat = new A();! else! waitwhat = new B();! whatwhat.method()! Where do we look up method? Lexical scoping rules don't help us here, because lexical scoping resolves all variable/function references at compile-time. At compile-time, we don't know what's going to happen.

Takeaway: Looking up the value for a field or the code for a method is different looking up a "regular" variable or "regular" function.

Java method lookup The semantics for method calls e0.m(e1,,en) 1. Evaluate e0, e1,, en to objects obj0, obj1,, objn As usual, may involve looking up this, variables, fields, etc. 2. Let C = the class of obj0 (every object has a class) 3. [Complicated rules to pick "the best m" using the types of e0, e1,, en] Rules similar to Ruby except for this static overloading 4. Evaluate body of method picked: With formal arguments bound to obj1,, objn With this bound to obj0 -- this implements dynamic dispatch!

The punch-line again e0.m(e1,,en) To implement dynamic dispatch, evaluate the method body with this mapping to the receiver That way, any this calls in the body use the receiver's class, everything works.

Comments on dynamic dispatch C++ only uses dynamic dispatch on virtual functions. That's why in that weird example from a few weeks ago we had the "wrong" function being called. Java always does dynamic dispatch. More complicated than the rules for closures Have to treat this specially May seem simpler only because you learned it first Complicated doesn't imply superior or inferior Depends on how you use it Overriding does tend to be overused

The OOP trade-off Any method that makes calls to overridable methods can have its behavior changed in subclasses even if it is not overridden Maybe on purpose, maybe by mistake Makes it harder to reason about "the code you're looking at" Can avoid by disallowing overriding (Java final) of helper methods you call Makes it easier for subclasses to specialize behavior without copying code Provided method in superclass isn't modified later

Manual dynamic dispatch Rest of lecture: Write Racket code with little more than pairs and functions that acts like objects with dynamic dispatch Why do this? (Racket actually has classes and objects even though not everything is an object) Demonstrates how one language's semantics is an idiom in another language Understand dynamic dispatch better by coding it up Roughly similar to how an interpreter/compiler would do it