CSE 341: Programming Languages. Section AC with Nate Yazdani

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

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

Interfaces, Mixins, & Multiple Inheritance

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

Programming Languages

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

Object Oriented Programming in Python 3

Ruby: Object-Oriented Concepts

Rules and syntax for inheritance. The boring stuff


CSE 341, Autumn 2015, Ruby Introduction Summary

Introduction to Programming Using Java (98-388)

CSci 4223 Lecture 18 April 3, 2013 Topics: OOP vs. FP

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

Object-Oriented Design

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

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

CSE 341: Programming Languages

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

OBJECT ORIENTED PROGRAMMING

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

CEU s (Continuing Education Units) 12 Hours (i.e. Mon Thurs 5 9PM or Sat Sun 8AM 5PM)

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Lecture 2. Object Orientation

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

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

C++ Important Questions with Answers

CS 5142 Scripting Languages

CS 11 python track: lecture 4

Standard. Number of Correlations

CSCI 3155: Principles of Programming Languages Exercise sheet #14 28 June 2007

Object-Oriented Design (OOD) and C++

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

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

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

Chapter 14 Abstract Classes and Interfaces

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

CSE341: Programming Languages Spring 2017 Unit 7 Summary Dan Grossman, University of Washington

Practice for Chapter 11

Chapter 10 Classes Continued. Fundamentals of Java

Lecture 2. Object Orientation 1 / 51

Inheritance, Polymorphism and the Object Memory Model

CSCI 3155: Principles of Programming Languages Exercise sheet #12 26 June 2007

CSE341, Fall 2011, Lecture 26 Summary

CS201 - Introduction to Programming Glossary By

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CSE au Final Exam Sample Solution

CS558 Programming Languages Winter 2013 Lecture 8

The Essence of OOP using Java, Nested Top-Level Classes. Preface

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

Objects, Subclassing, Subtyping, and Inheritance

CS111: PROGRAMMING LANGUAGE II

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Lecture 2. Object Orientation 1 / 50

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Exam in TDDB84: Design Patterns,

CPS 506 Comparative Programming Languages. Programming Language

CSE341, Spring 2013, Final Examination June 13, 2013

TDDB84: Lecture 09. SOLID, Language design, Summary. fredag 11 oktober 13

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

Object-Oriented Programming

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

Comp 249 Programming Methodology

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

In fact, as your program grows, you might imagine it organized by class and superclass, creating a kind of giant tree structure. At the base is the

Exception Handling. Chapter 9

BBM 102 Introduction to Programming II Spring Inheritance

Advances in Programming Languages

Short Notes of CS201

Subclassing for ADTs Implementation

Module 10 Inheritance, Virtual Functions, and Polymorphism

15.1 Origins and Uses of Ruby

Programming Languages & Paradigms PROP HT Abstraction & Modularity. Inheritance vs. delegation, method vs. message. Modularity, cont d.

6.005 Elements of Software Construction Fall 2008

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Principles of Ruby Applica3on Design. Dean Wampler Senior Mentor and Consultant Object Mentor, Inc. Chicago, IL

CSE341 Spring 2017, Final Examination June 8, 2017

Tecniche di Progettazione: Design Patterns

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

P2: Collaborations. CSE 335, Spring 2009

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

CSE 331 Final Exam 3/12/12

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

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

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Ruby Containers, Blocks, and Procs

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

CSc 372 Comparative Programming Languages

Multiple Inheritance using modules in Ruby

Inheritance. CSc 372. Comparative Programming Languages. 28 : Ruby Classes. Department of Computer Science University of Arizona.

CSc 372 Comparative Programming Languages

NOTES ON OBJECT-ORIENTED MODELING AND DESIGN

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Transcription:

CSE 341: Programming Languages Section AC with Nate Yazdani

aga method dispatch mixins visitor pattern

method dispatch

what is dispatch method dispatch or just dispatch is the protocol to look up the method body for a method call based on the classes of argument values the Ruby and Java languages incorporate single dispatch on the receiver object, the implicit self or this parameter secretly, class constructors stash references to their method implementations inside all objects single dispatch is what you learned in CSE 143

what is dispatch resolution must (usually) happen at runtime method dispatch or just dispatch is the protocol to look up the method body for a method call based on the classes of argument values the Ruby and Java languages incorporate single dispatch on the receiver object, the implicit self or this parameter secretly, class constructors stash references to their method implementations inside all objects single dispatch is what you learned in CSE 143

multiple dispatch double dispatch is when the look-up protocol uses both the receiver object and some other parameter neither Ruby nor Java have built-in support for this we can emulate it, though, and in fact, we will on the next homework! double dispatch generalizes to using any number of parameters, and this general concept is multiple dispatch or multimethods

emulating multimethods you can emulate multiple dispatch by using single dispatch once for each parameter for dispatch strategy: a method m in each class k calls a specialized method m k on the next parameter for dispatch, passing the remaining parameters and its own receiver object (e.g., self) each method in such a sequence (e.g., m, m k, m k,k, ) knows the class for one more argument, so the last method will know the class of every argument other languages provide different mechanisms to accomplish the same outcome, like nested case expressions in SML

emulating multimethods you can emulate multiple dispatch by using single dispatch once for each parameter for dispatch strategy: a method m in each class k calls a specialized method m k on the next parameter for dispatch, passing the remaining parameters and its own receiver object (e.g., self) each method in such a sequence (e.g., m, m k, m k,k, ) knows the class for one more argument, so the last method will know the class of every argument induction? other languages provide different mechanisms to accomplish the same outcome, like nested case expressions in SML

quick demo ]

example class A def f x x.f_a self class B def f x x.f_b self def f_a a puts (A, A) def f_a a puts (A, B) def f_b b puts (B, A) def f_b b puts (B, B)

triple dispatch! exercise: ext the previous example to do triple dispatch on arguments x, y, and z, printing (C x, C y, C z ), where C x is the class of x, C y is the class of y, and C z is the class of z (either A or B by assumption) bonus: can you find a way to achieve the same result using reflection instead of multiple dispatch?

mixins

mixins a mixin is just a collection of methods like class, but you can t instantiate it languages with mixins typically allow inheritance from one superclass and inclusion of any number of mixins solves most use cases for multiple inheritance without all the pain including a mixin adds its methods to the class prioritized by order of inclusion, so later mixins can override methods from earlier ones mixin methods can access self like any other method

example module Doubler def double self + self class String include Doubler class Numeric include Doubler class Point include Doubler attr_accessor :x, :y def initialize(x=0, y=0) @x = x; @y = y def + other Point.new(self.x + other.x, self.y + other.y)

quick demo ]

mixin method dispatch with mixins, need to revise the protocol for method look-up to find the right method body for a call o.m, look in the following places for a method with the name m: 1. the class of o 2. the mixins of the class of o, in order 3. the superclass of o 4. the mixins of the superclass of o, in order 5. instance variables work the same, because mixin and normal methods can interact with the same object however, bad style for mixins to use private instance variables, because their names might conflict with a class s!

common mixins two of the more popular and useful Ruby mixins: Comparable, which implements <, >, ==,!=, >=, and <= in terms of <=> Enumerable, which implements iterators like map and find in terms of each these are emblematic of the spirit of mixins classes implement simple core functionality (e.g., <=> and each), and mixins provide convenient abstractions on top classes don t have to waste their one superclass on some utility functionality!

visitor pattern

visitor pattern the visitor pattern is a design pattern in objectoriented programming for functional composition in object-oriented programming, code is grouped into classes we sometimes want to group code by their functionality, to make adding another operation easier in the future the visitor pattern is an application of double dispatch! commonly used with abstract syntax trees (a language implementation s representation of program syntax)

visitor pattern spread functionality across operand types the visitor pattern is a design pattern in objectoriented programming for functional composition in object-oriented programming, code is grouped into classes we sometimes want to group code by their functionality, to make adding another operation easier in the future the visitor pattern is an application of double dispatch! commonly used with abstract syntax trees (a language implementation s representation of program syntax)

quick demo ]