38
13. A bit of Smalltalk

13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap The origins of Smalltalk What is Smalltalk? Syntax in a nutshell Seaside — web development

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

13. A bit of Smalltalk

Page 2: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 2

Roadmap

> The origins of Smalltalk> What is Smalltalk?> Syntax in a nutshell> Seaside — web development with Smalltalk

Smalltalk

Page 3: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 3

Roadmap

> The origins of Smalltalk> What is Smalltalk?> Syntax in a nutshell> Seaside — web development with Smalltalk

Smalltalk

Page 4: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 4

The origins of Smalltalk

Alan Kay’s Dynabook project (1968)

Alto — Xerox PARC (1973)

gagne.homedns.org/~tgagne/contrib/EarlyHistoryST.html

Smalltalk

Page 5: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz

Smalltalk

11.5

Object-oriented language genealogy

Page 6: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz

Smalltalk

6

Smalltalk vs. C++ vs. Java

Smalltalk C++ Java

Object model Pure Hybrid Hybrid

Garbage collection Automatic Manual Automatic

Inheritance Single Multiple Single

Types Dynamic Static Static

Reflection Fully reflective Introspection Introspection

Concurrency Semaphores Some libraries Monitors

Modules Categories, namespaces

Namespaces Packages

Page 7: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 7

Smalltalk-80 and Squeak

• Everything is an object• Everything is there, all the time • First windowing system with mouse• First graphical IDE

Smalltalk

Page 8: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz

ST — Introduction

1.8

What are Squeak and Pharo?

> Squeak is a modern, open-source, highly portable, fast, full-featured Smalltalk implementation— Based on original Smalltalk-80 code

> Pharo is a lean and clean fork of Squeak— www.pharo-project.org

Page 9: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 9

Don’t panic!

New Smalltalkers often think they need to understand all the details of a thing before they can use it.

Try to answer the question

with

—Alan Knight. Smalltalk Guru

Smalltalk

“How does this work?”

“I don’t care”.

Page 10: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 10

Roadmap

> The origins of Smalltalk> What is Smalltalk?> Syntax in a nutshell> Seaside — web development with Smalltalk

Smalltalk

Page 11: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz

Smalltalk

11

Two rules to remember

Page 12: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

Everything is an object

© Oscar Nierstrasz 12

Smalltalk

Page 13: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

Everything happens by sending messages

© Oscar Nierstrasz 13

Smalltalk

Page 14: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 14

What is Smalltalk?

Image

Changes

+

Virtual machine

Sources

+

Smalltalk

Page 15: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 15

Running Pahro

Smalltalk

Page 16: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz

ST — Introduction

1.16

Mouse Semantics

Select

Operate

Window

Page 17: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz

ST — Introduction

1.17

World Menu

Page 18: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 18

Standard development tools

Smalltalk

Page 19: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 19

Debuggers, Inspectors, Explorers

Smalltalk

Page 20: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 20

Do it, Print it, …

You can evaluate any expression anywhere in Smalltalk.

Smalltalk

Page 21: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 21

Roadmap

> The origins of Smalltalk> What is Smalltalk?> Syntax in a nutshell> Seaside — web development with Smalltalk

Smalltalk

Page 22: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 22

Three kinds of messages

> Unary messages

> Binary messages

> Keyword messages

5 factorialTranscript cr

3 + 4

3 raisedTo: 10 modulo: 5

Transcript show: 'hello world'

Smalltalk

Page 23: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 23

Precedence

2 raisedTo: 1 + 3 factorial

1 + 2 * 31 + (2 * 3)

128

9 (!)7

First unary, then binary, then keyword:

Use parentheses to force order:

2 raisedTo: (1 + (3 factorial))Same as:

Smalltalk

Page 24: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 24

A typical method in the class Point

<= aPoint "Answer whether the receiver is neitherbelow nor to the right of aPoint."

^ x <= aPoint x and: [y <= aPoint y]

(2@3) <= (5@6) true

Method name Argument Comment

Return

Keyword messageInstance variable

BlockBinary message

Smalltalk

Page 25: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 25

Statements and cascades

| p pen |p := [email protected] := Pen new.pen up.pen goto: p; down; goto: p+p

Temporary variablesStatement

Cascade

Smalltalk

Assignment

Page 26: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 26

Literals and constants

Strings & Characters 'hello' $a

Numbers 1 3.14159

Symbols #yadayada

Arrays #(1 2 3)

Pseudo-variables self super

Constants true false

Smalltalk

Page 27: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 27

Method Return

> Use a caret to return a value from a method or a block

> By default, methods return self

max: aNumber^ self < aNumber

ifTrue: [aNumber] ifFalse: [self] 1 max: 2 2

Smalltalk

Page 28: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 28

Blocks

> Use square brackets to delay evaluation of expressions

^ 1 < 2 ifTrue: ['smaller'] ifFalse: ['bigger']

'smaller'

Smalltalk

Page 29: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 29

Variables

> Local variables are delimited by |var|Block variables by :var|

OrderedCollection>>collect: aBlock "Evaluate aBlock with each of my elements as the argument."| newCollection |newCollection := self species new: self size.firstIndex to: lastIndex do:

[ :index |newCollection addLast: (aBlock value: (array at: index))].

^ newCollection

(OrderedCollection with: 10 with: 5) collect: [:each| each factorial ]

an OrderedCollection(3628800 120)

Smalltalk

Page 30: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 30

Control Structures

> Every control structure is realized by message sends

max: aNumber^ self < aNumber

ifTrue: [aNumber] ifFalse: [self]

4 timesRepeat: [Beeper beep]

Smalltalk

Page 31: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 31

Creating objects

> Class methods

> Factory methods

OrderedCollection newArray with: 1 with: 2

1@21/2

a Pointa Fraction

Smalltalk

Page 32: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 32

Creating classes

> Send a message to a class (!)

Number subclass: #ComplexinstanceVariableNames: 'real imaginary'classVariableNames: ''poolDictionaries: ''category: 'ComplexNumbers'

Smalltalk

Page 33: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz 33

Roadmap

> The origins of Smalltalk> What is Smalltalk?> Syntax in a nutshell> Seaside — web development with Smalltalk

Smalltalk

Page 34: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

Seaside — a Smalltalk web development platform

© Oscar Nierstrasz

Smalltalk

34

Page 35: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

Seaside demo

© Oscar Nierstrasz

Smalltalk

35

Page 36: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz

Smalltalk

36

What you should know!

What are the key differences between Smalltalk, C++ and Java?

What is at the root of the Smalltalk class hierarchy? What kinds of messages can one send to objects? What is a cascade? Why does 1+2/3 = 1 in Smalltalk? How are control structures realized? How is a new class created? What are categories for? What are Factory methods? When are they useful?

Page 37: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

© Oscar Nierstrasz

Smalltalk

37

Can you answer these questions?

Which is faster, a program written in Smalltalk, C++ or Java?

Which is faster to develop & debug, a program written in Smalltalk, C++ or Java?

How are Booleans implemented? Is a comment an Object? How would you check this? What is the equivalent of a static method in Smalltalk? How do you make methods private in Smalltalk? What is the difference between = and ==? If classes are objects too, what classes are they

instances of?

Page 38: 13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development

License

© Oscar Nierstrasz 38

Attribution-ShareAlike 2.5You are free:• to copy, distribute, display, and perform the work• to make derivative works• to make commercial use of the work

Under the following conditions:

Attribution. You must attribute the work in the manner specified by the author or licensor.

Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.

• For any reuse or distribution, you must make clear to others the license terms of this work.• Any of these conditions can be waived if you get permission from the copyright holder.

Your fair use and other rights are in no way affected by the above.

http://creativecommons.org/licenses/by-sa/2.5/

Smalltalk