Programming Languages Dan Grossman 2013

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Advertisements

CSE341: Programming Languages Lecture 23 OO vs. Functional Decomposition; Adding Operations & Variants; Double-Dispatch Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 22 OOP vs. Functional Decomposition; Adding Operators & Variants; Double-Dispatch Dan Grossman Spring 2013.
CSE 341 Programming Languages Dynamic Dispatch vs. Closures OOP vs. Functional Decomposition Wrapping Up Zach Tatlock Spring 2014.
Programming Languages Dan Grossman 2013 Classes and Objects.
Programming Languages Dan Grossman 2013 Datatype-Programming in Racket Without Structs.
Programming Languages Dan Grossman 2013 Optional: Dynamic Dispatch Manually in Racket.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 22 OOP vs
Programming Languages Dan Grossman 2013
Programming Languages Dan Grossman 2013
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
FUNCTIONS In C++.
Programming Languages Dan Grossman 2013
CSE 341 Section 9 Nick Mooney Spring 2017
Programming Languages Dan Grossman 2013
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Emily Leland (Not Nick) Spring 2017
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
Java Programming Language
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Introduction to Python
CSE 341 Section 9 Winter 2018 Adapted from slides by Eric Mullen, Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
Java Lesson 36 Mr. Kalmes.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Adapted from slides by Nicholas Shahan and Dan Grossman
slides created by Ethan Apter
Nicholas Shahan Spring 2016
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 22 OOP vs
CSE 341 Section 9 Fall 2017 Adapted from slides by Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 22 OOP vs
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

Programming Languages Dan Grossman 2013 Binary Methods with OOP: Double Dispatch

Dan Grossman, Programming Languages Example To show the issue: Include variants String and Rational (Re)define Add to work on any pair of Int, String, Rational Concatenation if either argument a String, else math Now just defining the addition operation is a different 2D grid: Worked just fine with functional decomposition -- what about OOP… Int String Rational Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages What about OOP? Starts promising: Use OOP to call method add_values to one value with other value as result class Add … def eval e1.eval.add_values e2.eval end Classes Int, MyString, MyRational then all implement Each handling 3 of the 9 cases: “add self to argument” class Int … def add_values v … # what goes here? end Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages First try This approach is common, but is “not as OOP” So do not do it on your homework A “hybrid” style where we used dynamic dispatch on 1 argument and then switched to Racket-style type tests for other argument Definitely not “full OOP” class Int def add_values v if v.is_a? Int Int.new(v.i + i) elsif v.is_a? MyRational MyRational.new(v.i+v.j*i,v.j) else MyString.new(v.s + i.to_s) end Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Another way… add_values method in Int needs “what kind of thing” v has Same problem in MyRational and MyString In OOP, “always” solve this by calling a method on v instead! But now we need to “tell” v “what kind of thing” self is We know that! “Tell” v by calling different methods on v, passing self Use a “programming trick” (?) called double-dispatch… Jan-Mar 2013 Dan Grossman, Programming Languages

Double-dispatch “trick” Int, MyString, and MyRational each define all of addInt, addString, and addRational For example, String’s addInt is for adding concatenating an integer argument to the string in self 9 total methods, one for each case of addition Add’s eval method calls e1.eval.add_values e2.eval, which dispatches to add_values in Int, String, or Rational Int’s add_values: v.addInt self MyString’s add_values: v.addString self MyRational’s add_values: v.addRational self So add_values performs “2nd dispatch” to the correct case of 9! [Definitely see the code] Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Why showing you this Honestly, partly to belittle full commitment to OOP To understand dynamic dispatch via a sophisticated idiom Because required for the homework To contrast with multimethods (optional) Optional note: Double-dispatch also works fine with static typing See Java code Method declarations with types may help clarify Jan-Mar 2013 Dan Grossman, Programming Languages