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

Similar documents
CSE 341: Programming Languages

CSE 413 Spring Introduction to Ruby. Credit: Dan Grossman, CSE341

CSE 341, Autumn 2015, Ruby Introduction Summary

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

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

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

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

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

Class 22: Inheritance

CSE 303: Concepts and Tools for Software Development

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

CS 5142 Scripting Languages

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Data Abstraction. Hwansoo Han

Interfaces, Mixins, & Multiple Inheritance

Principles of Programming Languages, 2

Chapter 13. Object Oriented Programming

Lecture Notes on Programming Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Object-Oriented Technology. Rick Mercer

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Overview of the Ruby Language. By Ron Haley

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

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

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

QUIZ. How could we disable the automatic creation of copyconstructors

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Java Object Oriented Design. CSC207 Fall 2014

QUIZ. How could we disable the automatic creation of copyconstructors

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

The Rise of OOP: Part 1 [The Early Years] Dina Lamdany

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

CSE : Python Programming

CPS 506 Comparative Programming Languages. Programming Language

VIRTUAL FUNCTIONS Chapter 10

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

Rules and syntax for inheritance. The boring stuff

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Inheritance. Transitivity

C++ Important Questions with Answers

Functional Programming. Introduction To Cool

Lecturer: William W.Y. Hsu. Programming Languages

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

CS312: Programming Languages. Lecture 21: JavaScript

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Lecture 16: Object Programming Languages

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

Late Binding; OOP as a Racket Pattern

Subtyping (Dynamic Polymorphism)

Object-Oriented Programming and Design

Programming Paradigms

CS153: Compilers Lecture 11: Compiling Objects

CSE 341: Programming Languages

Contents in Detail. Who This Book Is For... xx Using Ruby to Test Itself... xx Which Implementation of Ruby?... xxi Overview...

Chapter 10 :: Data Abstraction and Object Orientation

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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

CSE 341: Programming Languages

Ruby: Introduction, Basics

Scope. Chapter Ten Modern Programming Languages 1

Object Oriented Programming

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

1: Introduction to Object (1)

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

Closing the Case for Groovy (and Ruby, and Python)

Node.js Training JavaScript. Richard richardrodger.com

Lecture 15: Object-Oriented Programming

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

CSE 341: Programming Languages. Section AC with Nate Yazdani

PROGRAMMING LANGUAGE 2

ob-ject: to feel distaste for something Webster's Dictionary

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

CSE341, Fall 2011, Lecture 26 Summary

Dynamic Object-Oriented Programming with Smalltalk 1. Introduction

Back to ObjectLand. Contents at: Chapter 5. Questions of Interest. encapsulation. polymorphism. inheritance overriding inheritance super

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

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

CSE 505: Concepts of Programming Languages

Objects. object-oriented. programming.

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

CSE341: Programming Languages Lecture 26 Course Victory Lap. Dan Grossman Spring 2016

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Chapter 9 :: Data Abstraction and Object Orientation

Ruby: Introduction, Basics


COMP6700/2140 Objects

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Java Programming Lecture 7

Ch.9: Object-oriented programming

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Introducing Wybe a language for everyone

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

DOWNLOAD PDF BEGINNING C 3.0 AN INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

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

Review sheet for Final Exam (List of objectives for this course)

Programming II (CS300)

CSE 341: Programming Languages

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

Transcription:

A Bit of History Some notable examples of early object-oriented languages and systems: Sketchpad (Ivan Sutherland s 1963 PhD dissertation) was the first system to use classes and instances (although Sketchpad is an application, not a programming language) First object-oriented programming language: Simula I, then Simula 67, created by Ole-Johan Dahl and Kristen Nygaard at the Norwegian Computing Center in Oslo. Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80) Today: mature language paradigm. Some significant examples: C++, Java, C#, Python, Ruby. Alan Borning CSE341 Winter 2010 1

Ruby Why Ruby? Some basics of Ruby programs Syntax Classes, Methods Variables, fields, scope Dynamic typing The rep-loop, the main class, etc. Note: Read Thomas book chapters 1 9 (or free first edition 1 8) Skip/skim regexps and ranges Not every detail: focus on OO, dynamic typing, blocks, mixins Alan Borning CSE341 Winter 2010 2

Principal Properties of Ruby Pure object-oriented: all values are objects Class-based Dynamically typed Convenient reflection A good starting point for discussing what each of these means and what other languages look like. dynamically typed statically typed functional Scheme Haskell object-oriented Ruby Java Alan Borning CSE341 Winter 2010 3

Ruby vs. Smalltalk Smalltalk: language definition unchanged since 1980 (although lots of work on the environment and packages), is also pure OO, class-based, dynamically-typed. Smalltalk: tiny language (smaller than Scheme), elegant, regular, can learn whole thing Smalltalk: integrated into cool, malleable GUI environment Ruby: large language with a why not? attitude Ruby: scripting language (light syntax, some odd scope rules) Ruby: very popular, massive library support especially for strings, regular expressions, Ruby on Rails. Won t be our focus at all. Ruby: mixins (a cool, advanced OO modularity feature) Ruby: blocks, libraries encourage lots of FP idioms Alan Borning CSE341 Winter 2010 4

Really key ideas Really, everything is an object (with constructor, fields, methods) Every object has a class, which determines how the object responds to messages. Dynamic typing (everything is an object) Dynamic dispatch Sends to self (a special identifier; Java s this) Everything is dynamic evaluation can add/remove classes, add/remove methods, add/remove fields, etc. Blocks are almost first-class anonymous functions (later) Can convert to/from real lambdas (class Proc) (Also has some more Java/C like features loops, return, etc.) Alan Borning CSE341 Winter 2010 5

Lack of variable declarations If you assign to a variable in scope, it s mutation. If the variable is not in scope, it gets created (!) Scope is the method you are in Same with fields: an object has a field if you assign to it So different objects of the same class can have different fields (!) This cuts down on typing but catches fewer bugs (misspellings) A hallmark of scripting languages (an informal term) Alan Borning CSE341 Winter 2010 6

Protection? Fields are inaccessible outside of instance Define accessor/mutator methods as desired Use attr_read and attr_writer Good OO design: subclasses can override accessors/mutators Methods are public, protected, or private protected: only callable from class or subclass object private: only callable from self Later: namespace management, but no hiding Alan Borning CSE341 Winter 2010 7

Unusual syntax Just a few random things (keep your own mental list): Variables and fields are written differently @ for fields @@ for class fields (Java s static fields) Newlines often matter need extra semicolons, colons, etc. to put things on one line Message sends do not need parentheses (especially with 0 arguments) Operators like + are just message sends Class names must be capitalized Alan Borning CSE341 Winter 2010 8

Duck Typing If it walks like a duck and quacks like a duck, it s a duck. A method might think, I need an Octopus but really it only needs an object that has similar enough methods that it acts enough like a Octopus that the method works. Embracing duck typing: Methods that make method calls rather than assume the class of their argument. Plus: More code reuse, very OO approach What messages can some object receive is all that matters Minus: Almost nothing is equivalent x+x versus x*2 versus 2*x Callees may not want callers assuming so much Alan Borning CSE341 Winter 2010 9

Blocks and Iterators Many methods in Ruby take a block, which is a special thing separate from the argument list. They are used very much like closures in functional programming; can take 0 or more arguments (see examples) The preferred way for iterating over arrays, doing something n times, etc. They really are closures (can access local variables where they were defined). Useful on homework: each, possibly inject Useful in Ruby: many, many more Alan Borning CSE341 Winter 2010 10

Blocks vs. Procs These block arguments can be used only by the immediate callee via the yield keyword. If you really want a first-class object you can pass around, store in fields, etc., convert the block to an instance of Proc. lambda { x,y,z e} Instances of Proc have a method call This really is exactly a closure. Actually, there is a way for the caller to pass a block and the callee convert it to a Proc. This is what lambda does (just a method in Object that returns the Proc it creates) Alan Borning CSE341 Winter 2010 11

Subclasses Ruby is dynamically typed, so subclassing is not about what type-checks. Subclassing is about inheriting methods from the superclass. In Java, it s about inheriting fields too, but we can just write to any field we want. Example: ThreeDPoint inherits methods x and y. Example: ColorPoint inherits distfromorigin and distfromorigin2. Alan Borning CSE341 Winter 2010 12

Overriding If it were just inheritance, then with dynamic typing subclassing would just be avoiding copy/paste. It s more. But first, simple overriding lets us redefine methods in the subclass. Often convenient to use super to use superclass definition in our definition. This is still just avoiding copy-paste. Example: distfromorigin and initialize in ThreeDPoint. Alan Borning CSE341 Winter 2010 13

Ruby-ish Digression Why make a subclass when we could just add/change methods to the class itself? Add a color field to Point itself Affects all Point instances, even those already created (!) Plus: Now a ThreeDPoint has a color field too. Minus: Maybe that messes up another part of your program. Fun example: Redefining Fixnum s + to return 5. Alan Borning CSE341 Winter 2010 14

Late-Binding So far, this OO stuff is very much like functional programming Fields are just like things in a closure s environment (remember simulating objects in Scheme) But this is totally different: When a method defined in a superclass makes a self call it resolves to the method defined in the subclass (typically via overriding) Example: distfromorigin2 in PolarPoint still works correctly!!! Coming up soon: Studying this very carefully. Alan Borning CSE341 Winter 2010 15