Guru 101 Grokking the Smalltalk system. Smalltalk Solutions Europe 1998 Joseph Pelrine Daedalos Consulting Group

Similar documents
Squeak Object Model. Technion - Israel Institute of Technology. Updated: Spring Object-Oriented Programming 1

Eliminating Procedural Code

Object. Accessing. Changing. Chapter

The Smalltalk class hierarchy

CHAIN OF RESPONSIBILITY (DP 223)

9. Understanding Classes and Metaclasses

An Introduction to Smalltalk for Objective-C Programmers

Basic Objects, Conditionals and Loops

The DCI Paradigm Implemented in Squeak

XXXXXXXXXXXXXXXXXXXXXXX. Evolution of Software Languages

Lord of the Base Image - Things every Library Supervisor should know

Working with Bytecodes: IRBuilder and InstructionStream. Marcus Denker. Reasons for working with Bytecode

OO design. Classes, Responsibilities, Collaborations (CRC) 13/9/1999 COSC

ST Introduction. Birds-eye view

There are a few important ways that Smalltalk is different then other languages:

Efficient Proxies in Smalltalk

Smalltalk. Topics. History of Smalltalk. OOP and GUI. Steve Jobs, PARC, Dec Smalltalk 1. The best way to predict the future is to invent it.

Smalltalk Implementation

Safely Extending the Environment Part 2 - being a good citizen Joseph Pelrine Daedalos Consulting

Smalltalk FOOP. Smalltalk

Abstract Interpretation for Dummies

Template Method Structure. Template Method Example. Template Method Example. Template Method Example. Benefits of Template Method

Smalltalk Best Practice Patterns. Part I

From Design to Implementation

Syntax and Messages. Stéphane Ducasse 8.1

LATENT METHODS. Richard CS

This chapter describes some of the more common errors that Smalltalk programmers make and gives suggestions on how to prevent the errors.

About Instance Initialization

Building a Gopher from Sockets and Widgets

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

Prototyping Languages Related Constructs and Tools with Squeak

Pharo Syntax in a Nutshell

Adriaan van Os ESUG Code Optimization

OO Design with Smalltalk a Pure Object Oriented Language and Environment

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

The System Transcript, Class Point and Inspectors

Ghost: A Uniform and General-Purpose Proxy Implementation

University of Berne Institute of Computer Science

Object Model. Object Oriented Programming Spring 2015

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Object-Oriented Thinking

2. Classes & Inheritance. Classes. Classes. Éric Tanter Objects and Design in Smalltalk. How do you create objects?

ST Introduction. Birds-eye view

Idioms for Building Software Frameworks in AspectJ

Object Oriented Paradigm Languages

Roos Instruments, Inc. RTALK - SMALLTALK ON THE JVM

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

Derived and abstract data types. TDT4205 Lecture 15

INVAIDER: A FRAMEWORK FOR VISUAL PROGRAMMING LANGUAGES. B.S., University of Illinois, 1990 THESIS. Submitted in partial fulllment of the requirements

CS354 gdb Tutorial Written by Chris Feilbach

The Smalltalk Environment, SUnit, and Inheritance

Refactorings. Refactoring. Refactoring Strategy. Demonstration: Refactoring and Reverse Engineering. Conclusion

A Lazy List Implementation in Squeak

Introduction to Smalltalk

:3002 Wilf 2017

Intro. Scheme Basics. scm> 5 5. scm>

Selected Design Patterns

Inside Smalltalk MVC: Patterns for GUI Programming

Rules and syntax for inheritance. The boring stuff

CPS 506 Comparative Programming Languages. Programming Language

P2: Collaborations. CSE 335, Spring 2009

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Outline Smalltalk Overview Pragmatic Smalltalk Closing. Pragmatic Smalltalk. David Chisnall. February 7,

3. A Simple Counter. Creating your own class

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

Beyond the Refactoring Browser: Advanced Tool Support for Software Refactoring

Why using Smalltalk for Teaching Object- Oriented Design

How Developers Use the Dynamic Features of Programming Languages: The Case of Smalltalk

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

A Fully Object-Oriented Exception Handling System: Rationale and Smalltalk Implementation

gnu Smalltalk Library Reference

2. The object-oriented paradigm!

Self-review Questions

A Browser for Incremental Programming

Chapter 5 Object-Oriented Programming

Copyright 1997 by Alec Sharp PDF-Conversion by Lukas Renggli Download more free Smalltalk-Books at:

Principles of Programming Languages. Objective-C. Joris Kluivers

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

INHERITANCE AND EXTENDING CLASSES

CS558 Programming Languages Winter 2013 Lecture 8

Object Model. Object Oriented Programming Winter

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Inheritance (Chapter 7)

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

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

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Some instance messages and methods

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

Running Pharo on the GemStone VM. James Foster VP of Finance & Operations, GemTalk Systems LLC ESUG 2017 Maribor, Slovenia 4 September 2017

1 Reflection and Metaprogramming in Smalltalk

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

UMBC CMSC 331 Final Exam

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

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

Chapter 1: Object-Oriented Programming Using C++

2. The object-oriented paradigm

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Transcription:

Guru 101 Grokking the Smalltalk system Smalltalk Solutions Europe 1998 Joseph Pelrine Daedalos Consulting Group jpelrine@daedalos.de

About this talk 10:00-12:00 AM Coffee break Beer afterwards Intermediate Assumes you know Smalltalk Not all answers are supplied 14.06.98 2

Introduction What is meta-level programming? Manipulating object state Manipulating object behavior Manipulating the environment Techniques & applications 14.06.98 3

My Goal I can t tell you all there is to know I can show you some basics, some structure, and some interesting things I can point you in the right direction, and let your curiosity lead you on 14.06.98 4

What Is Meta-level Programming? Meta-level programming is writing programs that manipulate not your objects, the way usual programs do, but the representations of your objects 14.06.98 5

The Good, the Bad, and the Ugly Smalltalk makes meta-level programming easy. Too easy, in fact When you meta program, you are no longer really programming in Smalltalk. The toolset doesn t support you that well. You can no longer read a line of code and guess what it does correctly 14.06.98 6

The First Rule of Meta-programming Save your image! 14.06.98 7

The Different Areas Manipulating object state Manipulating object behavior Manipulating the environment 14.06.98 8

Topic One - Manipulating Object State Object definitions Class definitions 14.06.98 9

Object Definitions and Instance Variables Objects tend to fall into two basic categories: Objects with indexed instance variables, such as arrays and strings Objects with named instance variables, such as person, with variables such as firstname, lastname and so on 14.06.98 10

Object Definitions and Instance Variables Or, looking at it in another way: Objects with pointer instance variables Objects with arrays of single bytes (or words, or longs) 14.06.98 11

Objects With Pointer Instance Variables Most instance variables are pointers, that is, they point to another object. The firstname instance variable of a person (normally) points to a string, the size variable of an orderedcollection points to an integer. The pointers do not need to be named. Indexed objects such as arrays can also store pointers 14.06.98 12

Objects With Arrays of Single Bytes Some objects store arrays of single bytes directly, like a string in C. Since arrays of bytes are needed frequently, this is much more efficient than storing an array of pointers, each pointing to a single byte 14.06.98 13

Class Types Indexed Pointers Creation method Examples No Yes #subclass Object, Behavior Yes Yes #variablesubclass Array,CompiledMethod Yes No #variablebytesubclass String, ByteArray No No (none) (none) The fourth combination of bytes/no index has no examples. Although possible, creating a class like this would mean that you want every byte to have its own name - a rather dubious benefit. 14.06.98 14

Restrictions on Subclasses As you may have noticed, classes are sensitive to how their subclasses are defined 14.06.98 15

Classes Defined With #subclass These can have any kind of subclass 14.06.98 16

Classes Defined With #variablesubclass These can only have subclasses which are #variablesubclass or #variablebytesubclass Once you use indexes, all subclasses must also use indexes 14.06.98 17

Classes Defined With #variablebytesubclass You can only use #variablebytesubclass to create a class if the superclass has no defined instance variables Pointer classes and byte classes don t mix Byte classes can only have subclasses which are also #variablebytesubclass Once you use bytes, all subclasses must also use bytes 14.06.98 18

Methods Relating to Class Type Class>>isPointers Class>>isBits Class>>isBytes Class>>isFixed Class>>isVariable Class>>kindOfSubclass 14.06.98 19

Accessing Instance Variables Directly Object>>at:, object>>basicat: For indexed objects, both byte and pointer, you can use the famous #at: method. If a class has overridden #at: (e.g. Dictionary), you can use #basicat: to do direct access Object>>instVarAt: To access named instances, use #instvarat:, which takes an integer index. The index is the offset of the variable in #allinstvarnames. #instvarat: does not work for byte objects 14.06.98 20

Methods Relating to Instance Variables Object>>size Object>>basicSize Class>>instVarNames Class>>instanceVariableString Class>>allInstVarNames Class>>instSize 14.06.98 21

Topic Two - Manipulating Object Behavior State flags State object / strategy object Pluggable selector Pluggable block Pluggable method dictionary 14.06.98 22

State Flags Use a boolean flag to differentiate between cases display ishighlighted iftrue: [self displayhighlighted] iffalse: [self displayunhighlighted] 14.06.98 23

State Object / Strategy Object Your object has a number of different states or algorithms Reliance on state or strategy often extends to several methods, creating a maintenance headache Case statements are ugly anyway Create an object for each state or strategy Hold the state or strategy object in an instvar Delegate to the state or strategy object 14.06.98 24

Pluggable Selector The simplest way to implement pluggable behavior is to store a selector to be performed Add a variable which holds the selector Append message or selector to the variable name Create a composed method which simply performs the selector 14.06.98 25

#perform: #perform: sends the receiver the message given as the argument The argument must be a symbol, but it can be put together at run-time There are variations of #perform: perform: asymbol perform: with: perform: with: with: perform: with: with: with: perform: witharguments: 14.06.98 26

Pluggable Selector Example An example for displaying an object hierarchy createviews childrenmessage := #sortedsubclasses getchildren objectlist notnil iftrue: [ objectlist do: [ :object object perform: #childrenmessage]]. self updatelistbox. 14.06.98 27

Pluggable Block How do you implement pluggable behavior that is not quite worth its own class? Add a variable which holds the block Append block to the variable name Create a composed method which simply evaluates the block 14.06.98 28

Pluggable Block Disadvantages Pluggable blocks come at a great cost You can t statically analyse the code to understand the flow of control Even inspecting the plugged object doesn t really help The only solution is to single step through the block invocation Blocks are difficult to store on external media Some object streams cannot store and retrieve blocks 14.06.98 29

Pluggable Block Example An example for displaying on a medium initialize displayblock := [ :amedium amedium black] displayon: amedium displayblock value: amedium 14.06.98 30

Pluggable Method Dictionary You have objects that need to change their logic (at runtime) You cannot anticipate all needed behavior You want an elegant solution. (Or you want to show off your Smalltalk skills ;-) 14.06.98 31

Solution Make each instance specializable. You or your users can change the meaning of any message without affecting any other instances You specialize an instance by giving it its own method dictionary 14.06.98 32

The Method Dictionary The method dictionary (really dictionaries) for a class is simply a dictionary with symbols (selectors) as the keys, and CompiledMethods (the actual code) as the values Behavior>>addSelector: asymbol withmethod: acompiledmethod Behavior>>removeSelector: asymbol 14.06.98 33

Method Names Note that the method names are just symbols - there are no absolute restrictions on method names. The only problem is getting the compiler to accept them. You could use an acceptable name for the compiler, but then rename the method when you add it to the method dictionary! 14.06.98 34

Accessing the Method Dictionary Behavior>>selectors Behavior>>includesSelector: asymbol Behavior>>canUnderstand: asymbol Behavior>>respondsTo: asymbol Behavior>>implementorsOf: asymbol Behavior>>compiledMethodAt: asymbol 14.06.98 35

#doesnotunderstand: If your class doesn t have a method in its dictionary, the VM looks in the method dictionary of the superclasses, all the way to object If object doesn t have the method in its dictionary, the VM sends #doesnotunderstand: amessage You can override # doesnotunderstand : to do what you want: doesnotunderstand: amessage ^self printstring perform: amessage selector witharguments: amessage arguments 14.06.98 36

Specialized Instances The way to keep changes from one instance from affecting the others of its class is simple: they don t all point to the same class object To be separately specializable, they need to point to different class objects, each of which inherits from the original class This way, methods installed for one instance are installed only in that instance s personal class, and not the common one 14.06.98 37

Essentially We insert an unnamed class in the hierarchy below the object s class, and make the object a member of this unnamed class How we do this depends on the dialect 14.06.98 38

VisualWorks In VisualWorks, every object has a hidden instance variable that holds its class The class has an instance variable that holds a method dictionary Point Class class x 55 name Point MethodDictionary y 77 methods values keys #+ #dist: CompiledMethod CompiledMethod 14.06.98 39

VisualWorks When the object is sent a message Its class is fetched The class MethodDictionary is fetched The selector of the message is looked up in the dictionary The CompiledMethod found there is activated 14.06.98 40

Instance Specialization Point class x 47 y 11 Point class x 23 y 42 name methods Class Point class x y class x y Point 47 11 Point 23 42 Behavior superclass methods Behavior superclass methods name methods Class Point 14.06.98 41

Instance Specialization If you want all instances of your class to be specializable, you can override #new new Create a speciaizable instance ^Behavior new superclass: self; format: self format; methoddictionary: MethodDictionary new; new Implement the following instance method to specialize behavior specialize: astring Compile astring as a method for this instance only self class compile: astring notifying: nil 14.06.98 42

Lazy Specialization You might only want to specialize when necessary. Use these methods for lazy specialization: specialize: astring Compile astring as a method for this instance only self specialize. self class compile: astring notifying: nil specialize class self isspecialized iftrue: [^self]. class := Behavior new superclass: self class; format: self class format; methoddictionary: MethodDictionary new. self changeclasstothatof: class basicnew isspecialized ^self class shouldberegistered 14.06.98 43

Visual Smalltalk In Visual Smalltalk, each instance has a reference, not to its class, but to an array of MethodDictionaries Each method dictionary has an instance variable called class, which is set to the class to which it belongs #class is implemented in Visual Smalltalk by running down the array of method dictionaries and finding the first one whose class field is set. You can change the class by changing the method dictionary array 14.06.98 44

Visual Smalltalk Point class Array MethodDictionary x y 55 77 values Class keys #+ #dist: CompiledMethod CompiledMethod name methods Point values keys MethodDictionary 14.06.98 45

Instance Specialization class x y Point 55 77 Array MethodDictionary Class Array MethodDictionary name methods Point MethodDictionary 14.06.98 46

Instance Specialization Any object can build the array of method dictionaries To specialize an instance, we: Fetch the method dictionary array Copy it, adding a slot at the beginning holding a fresh MethodDictionary instance The methods needed for this are already implemented 14.06.98 47

Visual Smalltalk Relevant methods addbehavior: MethodDictionary removebehavior: MethodDictionary reverttoclassbehavior methoddictionaries methoddictionaryarray methoddictionaryarray: anarray 14.06.98 48

Instance Specialization If you want all instances specializable: new ^super new specialize The other methods isspecialized ^self methoddictionaryarray == self class methoddictionaries specialize self isspecialized iftrue: [^self]. self addbehavior: MethodDictionary new. specialize: astring association self specialize. association := Compiler compile: astring in: self class. self methoddictionaryarray first add: association 14.06.98 49

Example partdoit: amethod witharguments: anarray Answer the result of evaluating the temporary instance method <amethod> with the argument values in <anarray> mdtemp result mdtemp := MethodDictionary new at: amethod selector put: amethod; yourself. self addbehavior: mdtemp. [result := self perform: amethod selector witharguments: anarray] ensure [self removebehavior: mdtemp]. ^result 14.06.98 50

The Difference In VisualWorks, you access the method dictionary through the class instvar In Visual Smalltalk, you access the class through the method dictionary 14.06.98 51

VisualAge The VisualAge programming model is similar to the VisualWorks model, with a few changes The new unnamed class must be subclassed from Class, not Behavior new Create a specializable instance ^Class new superclass: self; instanceshape: self instanceshape instvarnames: self instvarnames; setmethoddictionary: MethodDictionary new; new 14.06.98 52

VisualAge In VisualAge, both MethodDictionary and CompiledMethod are directly subclassed from object 14.06.98 53

Lazy Specialization Use these methods for lazy specialization: specialize: astring Compile astring as a method for this instance only self specialize. self class compile: astring notifying: nil specialize class self isspecialized iftrue: [^self]. class := Class new superclass: self class; instanceshape: self instanceshape instvarnames: self instvarnames; setmethoddictionary: MethodDictionary new. self changeclassto: class isspecialized ^self class name isnil 14.06.98 54

VisualAge In VisualAge, we need to define the following method in Object: changeclasstothatof: anobject self fixclassto: anobject class 14.06.98 55

Topic Three - Some Practical (and Not-so Practical) Applications Type-safe objects Code generation Metrics and code quality tools 14.06.98 56

Type-safe Objects You can t be serious! TypedValueHholder WorkingObject #doesnotunderstand: 14.06.98 57

TypedValueHolder Similar to PluggableAdaptor Instance variables value - the real object type - the object class getblock - a block for getting the value setblock - a block for setting the value validateblock - a block for validating possible new values 14.06.98 58

WorkingObject Instance variables variables - a dictionary of typedvalueholders realobject - the object that we re wrappering Methods Class #on: arealobject #updatedrealobject #doesnotunderstand: 14.06.98 59

#doesnotunderstand: doesnotunderstand: amessage "Private - See if the message selector is one of the field names of the receiver. If so, access that field. If not, see whether the real object understands the message. If so, let him respond to it. Else scream" field s key s := amessage selector. key := (s select: [ :c (c = $:) not ]) assymbol. field := variables at: key ifabsent: [ (self realobject respondsto: s) iftrue: [ ^(amessage receiver: self realobject) perform] iffalse: [ ^super doesnotunderstand: amessage]]. s last = $: iftrue: [^self privateat: key put: amessage arguments first] iffalse: [^self privateat: key]. 14.06.98 60

Metrics Metrics are measures of important properties of the software being tested Metrics use the following meta-level facilities Class and class hierarchy queries Method queries Possible applications include Statistical analysis Project management 14.06.98 61

Metrics Metrics can be used to understand and predict the quality and cost of developing and maintaining large computer systems They can also be used to determine qualitative differences between different development projects If a particular library contains an average of 60 methods per class but the average over a number of projects is 20, DON'T YOU WANT TO KNOW WHY? 14.06.98 62

Different Kinds of Metrics There are two major categories of metric measurements: Static measures (time-independent) Easily obtained from Smalltalk because of meta-level facilities Dynamic measures (time-dependent) Best gathered in the context of team development tools like ENVY/developer and Team/V. This includes: Run-time measures (requires code execution) Evolutionary measures (requires histories over time) W. LaLonde, J. Pugh, Gathering metric information using metalevel facilities, Journal of object-oriented programming, March-April 1994 14.06.98 63

What Not to Do ;-) Before this course Hello World edit After this course [(Smalltalk at: #PendingEvents) perform: (OrderedCollection compiledmethodat: #add:) with: (Message new receiver: ( #($H $e $l $l $o $ $W $o $r $l $d inject: into:[ :sum :value sum, value asstring]) selector: ( tide reverse assymbol) arguments: #())] fork 14.06.98 64

Summary Meta-level programming can be fun! Just because you can do it doesn t mean that you must do it Take your time (you ll need a lot of it anyway) 14.06.98 65

Caveat Meta-programming is important for mastery of Smalltalk, but it is not important for effective engineering. In fact, it's far more important to know when NOT to use it than it is to know how TO use it. - Kent Beck 14.06.98 66