CSE 341 Section 9 Fall 2017 Adapted from slides by Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 23 OO vs. Functional Decomposition; Adding Operations & Variants; Double-Dispatch Dan Grossman Fall 2011.
Advertisements

CSE341: Programming Languages Lecture 21 Late Binding; OOP as a Racket Pattern Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Winter 2014.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CSE341: Programming Languages Lecture 22 OOP vs. Functional Decomposition; Adding Operators & Variants; Double-Dispatch Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
Ruby Inheritance and other languages….
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 22 OOP vs
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 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
CSE 341 Section 9 Nick Mooney Spring 2017
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 22 OOP vs
Emily Leland (Not Nick) Spring 2017
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Ruby: Blocks & Procs; Inheritance & Overriding Alan Borning Spring 2018.
Teach A-Level Computer Science: Object-Oriented Programming in Python
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 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
CSE 341 Section 9 Winter 2018 Adapted from slides by Eric Mullen, Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
Nicholas Shahan Spring 2016
CSE341: Programming Languages Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins Alan Borning Spring 2018.
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 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
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.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
Java Programming Course
Spencer Pearson Spring 2017
CSE 341 Section 2 Nick Mooney Spring 2017
Adapted from slides by Nicholas Shahan and Dan Grossman
Nicholas Shahan Spring 2016
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Dan Grossman / Eric Mullen Autumn 2017
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 22 OOP vs
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
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 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

CSE 341 Section 9 Fall 2017 Adapted from slides by Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman

Today’s Agenda Double Dispatch Again Mixins The Visitor Pattern

Dispatch Overview Dispatch is the runtime procedure for looking up which function to call based on the parameters given: Ruby (and Java) use Single Dispatch on the implicit self parameter Uses runtime class of self to lookup the method when a call is made This is what you learned in CSE 143 Double Dispatch uses the runtime classes of both self and a single method parameter Ruby/Java do not have this, but we can emulate it This is what you will do in HW7 You can dispatch on any number of the parameters and the general term for this is Multiple Dispatch or Multimethods

Emulating Double Dispatch To emulate double dispatch in Ruby (on HW7) just use the built-in single dispatch procedure twice! Have the principal method immediately call another method on its first parameter, passing self as an argument The second call will implicitly know the class of the self parameter It will also know the class of the first parameter of the principal method, because of Single Dispatch There are other ways to emulate double dispatch Found as an idiom in SML by using case expressions

Double Dispatch Example class A def f x x.fWithA self end def fWithA a "(a, a) case" def fWithB b "(b, a) case" class B def f x x.fWithB self end def fWithA a "(a, b) case" end def fWithB b "(b, b) case"

Mixins A mixin is (just) a collection of methods Less than a class: no instances of it Languages with mixins (e.g., Ruby modules) typically let a class have one superclass but include any number of mixins Semantics: Including a mixin makes its methods part of the class Extending or overriding in the order mixins are included in the class definition More powerful than helper methods because mixin methods can access methods (and instance variables) on self not defined in the mixin

Mixin Example module Doubler def double self + self # assume included in classes w/ + end class String include Doubler class AnotherPt attr_accessor :x, :y def + other ans = AnotherPt.new ans.x = self.x + other.x ans.y = self.y + other.y ans

Method Lookup Rules Mixins change our lookup rules slightly: When looking for receiver obj's method m, look in obj's class, then mixins that class includes (later includes shadow), then obj's superclass, then the superclass' mixins, etc. As for instance variables, the mixin methods are included in the same object So usually bad style for mixin methods to use instance variables since names can clash

The Two Big Ones The two most popular/useful mixins in Ruby: Comparable: Defines <, >, ==, !=, >=, <= in terms of <=> http://ruby-doc.org/core-2.2.3/Comparable.html Enumerable: Defines many iterators (e.g., map, find) in terms of each http://ruby-doc.org/core-2.2.3/Enumerable.html Great examples of using mixins: Classes including them get a bunch of methods for just a little work Classes do not “spend” their “one superclass” for this Does not bring on the complexity of multiple inheritance

The Visitor Pattern A template for handling a functional composition in OOP OOP wants to group code by classes We want code grouped by functions This makes it easier to add operations at a later time. Relies on Double Dispatch!!! Dispatch based on (VisitorType, ValueType) pairs. Often used to compute over AST’s (abstract syntax trees) Heavily used in compilers