119
http://www.pharo-project.org / A clean, innovative, open-source Smalltalk Serge Stinckwich [email protected]

Pharo, an innovative and open-source Smalltalk

Embed Size (px)

Citation preview

Page 2: Pharo, an innovative and open-source Smalltalk

1 Smalltalk Syntax

Smalltalk object model

Pharo, an open-source Smalltalk

2

3

Page 3: Pharo, an innovative and open-source Smalltalk

Smalltalk syntax1

Page 4: Pharo, an innovative and open-source Smalltalk

What are the keywords ?

How do you build expressions ?

Page 5: Pharo, an innovative and open-source Smalltalk

Almost no keywords

Page 6: Pharo, an innovative and open-source Smalltalk

Syntax for litterals

1 -10 30

3.14156 1e-10 -1.4e4

$A $z

‘Hello World’

#(1 2 3 4 5)

Page 7: Pharo, an innovative and open-source Smalltalk

“A comment is a sequence of characters surrounded by

quotation marks.”

Page 8: Pharo, an innovative and open-source Smalltalk

Assignement

x := 3.y := ‘Hello world’.z := x+4.

Page 9: Pharo, an innovative and open-source Smalltalk

6 pseudo-variables

nil, true, false

self, super, thisContext

Page 10: Pharo, an innovative and open-source Smalltalk

Everything happensby message sending

Page 11: Pharo, an innovative and open-source Smalltalk

(1) Unary message

Page 12: Pharo, an innovative and open-source Smalltalk

receiver message.

Page 13: Pharo, an innovative and open-source Smalltalk

20 factorial

Page 14: Pharo, an innovative and open-source Smalltalk

2432902008176640000

Page 15: Pharo, an innovative and open-source Smalltalk

‘Esope reste et se repose’reversed

Page 16: Pharo, an innovative and open-source Smalltalk

‘esoper es te etser eposE’

Page 17: Pharo, an innovative and open-source Smalltalk

‘12345’ isAllDigits

Page 18: Pharo, an innovative and open-source Smalltalk

true

Page 19: Pharo, an innovative and open-source Smalltalk

‘Quelle est ma longueur ?’ size

Page 20: Pharo, an innovative and open-source Smalltalk

23

Page 21: Pharo, an innovative and open-source Smalltalk

‘foobar’ first

Page 22: Pharo, an innovative and open-source Smalltalk

$f

Page 23: Pharo, an innovative and open-source Smalltalk

(2) Binary Messages

Page 24: Pharo, an innovative and open-source Smalltalk

aReceiver aSelector anArgument

Page 25: Pharo, an innovative and open-source Smalltalk

3 + 4

Page 26: Pharo, an innovative and open-source Smalltalk

7

Page 27: Pharo, an innovative and open-source Smalltalk

‘Bonjour ’, ‘monde’

Page 28: Pharo, an innovative and open-source Smalltalk

‘Bonjour monde’

Page 29: Pharo, an innovative and open-source Smalltalk

2@3

Page 30: Pharo, an innovative and open-source Smalltalk

2@3

Page 31: Pharo, an innovative and open-source Smalltalk

(3) Keywords Messages

Page 32: Pharo, an innovative and open-source Smalltalk

12 between: 8 and:15.

Page 33: Pharo, an innovative and open-source Smalltalk

8 < 12 < 15 ?

Page 34: Pharo, an innovative and open-source Smalltalk

true

Page 35: Pharo, an innovative and open-source Smalltalk

#(1 4 9 16 25) at: 3

Page 36: Pharo, an innovative and open-source Smalltalk

9

Page 37: Pharo, an innovative and open-source Smalltalk

aString.substring(2, 5) aString copyFrom: 2 to: 5

new ColorValue(a, b, c) ColorValue hue: a saturation: b value: c

new DateAndTime(a, b, c) DateAndTime year: a day: b timeZone: c

Easier to document the semantic roleof arguments with keywords messages.

Page 38: Pharo, an innovative and open-source Smalltalk

(Msg) > Unary > Binary > Keywords

Page 39: Pharo, an innovative and open-source Smalltalk

Messages cascading

Page 40: Pharo, an innovative and open-source Smalltalk

myBirthday := Date new.

myBirthDay setYear: 1967. myBirthDay setMonth: 5.myBirthDay setDayOfMonth: 10.

Page 41: Pharo, an innovative and open-source Smalltalk

myBirthday := Date new; setYear: 1967; setMonth: 5; setDayOfMonth: 10.

Page 42: Pharo, an innovative and open-source Smalltalk

TheEnd

Page 43: Pharo, an innovative and open-source Smalltalk

if ... then ... else ?

Page 44: Pharo, an innovative and open-source Smalltalk

Weather today isRaining

ifTrue: [self takeMyUmbrella]

ifFalse: [self takeMySunglasses]

ifTrue:ifFalse is sent to an objet: a boolean

Page 45: Pharo, an innovative and open-source Smalltalk

Closures

[3+4]

[‘Bonjour’ size. 3+4. #(1 2 3 4) at:2]

Page 46: Pharo, an innovative and open-source Smalltalk

A block (lexical closure) is a Smalltalk object, whose value could be evaluated in

the future.

A block is evaluated by sending the message value.

Page 47: Pharo, an innovative and open-source Smalltalk

[1+2] value. ⇒ 3

Page 48: Pharo, an innovative and open-source Smalltalk

Block could be parametrized

[:param1 :param2 ... | statements]

Page 49: Pharo, an innovative and open-source Smalltalk

[:x | x+1] value:2.

[:x :y | x,y] value:’Bonjour’ value:’monde !’.

Page 50: Pharo, an innovative and open-source Smalltalk

Dynamic binding

Postponing selection of an operation until execution time

aCondition ifTrue:[y:= 3] ifFalse:[y:= #(1 2 3)].y printOn: someStream.

Many printOn: methods,compiler can’t preordain the choice.

Page 51: Pharo, an innovative and open-source Smalltalk

Dynamic binding enables polymorphism

Page 52: Pharo, an innovative and open-source Smalltalk

Where is implemented ifTrue:ifFalse: ?

Page 53: Pharo, an innovative and open-source Smalltalk

Where is implemented ifTrue:ifFalse: ?

True>>ifTrue:aBlock1 ifFalse: aBlock2

^aBlock1 value

False>>ifTrue:aBlock1 ifFalse: aBlock2

^aBlock2 value

Page 54: Pharo, an innovative and open-source Smalltalk

Build your own control structure !

7 ifSeven:[Object inform: ‘Its seven’]

Number>>ifSeven:aBlock

self=7 ifTrue:[^aBlock value]

Page 55: Pharo, an innovative and open-source Smalltalk

Iterations

Page 56: Pharo, an innovative and open-source Smalltalk

[Weather today isRaining]whileTrue:[self doNotGoOutside.

self readAGoodBook]

Page 57: Pharo, an innovative and open-source Smalltalk
Page 58: Pharo, an innovative and open-source Smalltalk

| life |life := #(calvin hates suzie).life at:2 put:#loves.life. ⇒ #(#calvin #loves #suzie)

life first. ⇒ #calvinlife last. ⇒#suzielife indexOf:#calvin. ⇒ 1life indexOf:#asterix ifAbsent:[Transcript show:’Je ne connais pas’].

Array

Page 59: Pharo, an innovative and open-source Smalltalk

s := Set new.s add:1.s add:2.s add:2.s. ⇒ a Set(1 2)

Set

Page 60: Pharo, an innovative and open-source Smalltalk

Interval

-10 to:10. ⇒ (-10 to: 10)(-10 to:10) at:2. ⇒ -9(-10 to:10) at:2 put:3. ⇒ erreur

Page 61: Pharo, an innovative and open-source Smalltalk

OrderedCollection

distributions := OrderedCollection new.distributions add:’Slackware’;

add:’Fedora’; add:’Ubuntu’.distributions. ⇒ an OrderedCollection('Slackware' 'Fedora' 'Ubuntu')distributions remove:’Fedora’.distributions. ⇒ an OrderedCollection('Slackware' 'Ubuntu')

Page 62: Pharo, an innovative and open-source Smalltalk

OrderedCollectiondistributions addFirst:’Debian’.distributions addLast:’RedHat’.

distributions. ⇒ an OrderedCollection('Debian' 'Slackware' 'Ubuntu' 'RedHat')

distributions add:’Mandriva’ afterIndex:2.

distributions. ⇒ an OrderedCollection('Debian' 'Slackware' 'Mandriva' 'Ubuntu' 'RedHat')

Page 63: Pharo, an innovative and open-source Smalltalk

Collection enumeration

A collection contains a lot of elements.

Enumerate a collection is browsing the collection and running a statement for each element:

do:, select:, reject:collect:, inject:

Page 64: Pharo, an innovative and open-source Smalltalk

Enumeration messages are polymorphic=

they work whatever the collection is

Page 65: Pharo, an innovative and open-source Smalltalk

do:

Evaluate the block for each element of the collection.

total := 0.a := #(1 2 3 4 5 6).a do:[:unElement |  total := total + unElement]

Page 66: Pharo, an innovative and open-source Smalltalk

select:

Evaluate the block for each element of the collection and return a collectiof the items of the same class containing elements whose evaluation return true.

a := #(1 2 3 4 5 6).a select:[:unElement| unElement even]

Return : #(2 4 6)

Page 67: Pharo, an innovative and open-source Smalltalk

detect:

Evaluate the block for each element of the collection and return the first element that evaluate as the block value as true.

a := #(1 2 3 4 5 6).a detect:[:unElement |  unElement>3].

Return : 4.

Page 68: Pharo, an innovative and open-source Smalltalk

Smalltalk object model2

Page 69: Pharo, an innovative and open-source Smalltalk

Classe Rectangle

Rectangle

widthheight

area...

Page 70: Pharo, an innovative and open-source Smalltalk

Object subclass: #Rectangle instanceVariableNames: 'width height' classVariableNames: '' poolDictionaries: '' category: 'Exemples-Heritage'

Page 71: Pharo, an innovative and open-source Smalltalk

Rectangle>>width: wwidth := w

Rectangle>>height: hheight := h

Rectangle>>width^width

Rectangle>>height^height

Rectangle>>area^ width*height

Page 72: Pharo, an innovative and open-source Smalltalk

Classe ColoredRectangle

ColoredRectangle

widthheightcolor

area...

Page 73: Pharo, an innovative and open-source Smalltalk

A colored rectangle is like a rectangle but with a color ...

Page 74: Pharo, an innovative and open-source Smalltalk

Operations that can be done a rectangle, can also be done on a colored rectangle

(e.g surface calculus)

Page 75: Pharo, an innovative and open-source Smalltalk

a ColoredRectangle isa Rectangle

Page 76: Pharo, an innovative and open-source Smalltalk

Rectangle subclass: #ColoredRectangle instanceVariableNames: 'color' classVariableNames: '' poolDictionaries: '' category: 'Exemples-Heritage'

Page 77: Pharo, an innovative and open-source Smalltalk

ColoredRectangle>>color ^color

ColoredRectangle>>color: aColor color := aColor

Page 78: Pharo, an innovative and open-source Smalltalk

Class inheritanceRectangle

widthheight

area...

ColoredRectangle

color

...

Page 79: Pharo, an innovative and open-source Smalltalk

a := Rectangle new.a width:10.a height:50.

a width. ⇒ 10a height. ⇒ 50a area. ⇒ 500

Page 80: Pharo, an innovative and open-source Smalltalk

b := ColoredRectangle new.b width:10.b height:50.b color: Color blue.b width. ⇒ 10b height. ⇒ 50b color. ⇒ Color blueb area. ⇒ 500

Page 81: Pharo, an innovative and open-source Smalltalk

Rule 1Everything is an object

Rule 2Every object is an instance of one

classRule 3

Every class has a super-classRule 4

Everything happens by message sending

Page 82: Pharo, an innovative and open-source Smalltalk

Classes are also objects !

1 class⇒ SmallInteger

20 factorial class ⇒ LargePositiveInteger‘Hello’ class ⇒ ByteString

#(1 2 3) class ⇒ Array

Page 83: Pharo, an innovative and open-source Smalltalk

Every class has a superclass

Integer superclass ⇒ Integer

Number superclass ⇒ Magnitude

Magnitude superclass ⇒ Object

Page 84: Pharo, an innovative and open-source Smalltalk

doesNotUnderstand:

Page 85: Pharo, an innovative and open-source Smalltalk

If a class is an object, every class should be also an

instance of a specific classe.

Metaclasses !

Page 86: Pharo, an innovative and open-source Smalltalk
Page 87: Pharo, an innovative and open-source Smalltalk
Page 88: Pharo, an innovative and open-source Smalltalk

Metaclasses hierarchy is parallel to class hierarchy

Page 89: Pharo, an innovative and open-source Smalltalk
Page 90: Pharo, an innovative and open-source Smalltalk

Every metaclasses is instance of Metaclass

Page 91: Pharo, an innovative and open-source Smalltalk

The metaclass of Metaclass is an

instance of Metaclass

Page 92: Pharo, an innovative and open-source Smalltalk
Page 93: Pharo, an innovative and open-source Smalltalk

How many classes in the system ?

Object allSubclasses size

Page 94: Pharo, an innovative and open-source Smalltalk

Object allSubclasses do:[:aClass| aClass methodDict keys select: [:aMethod | (aClass>>aMethod) isAbstract ]]

How many abstract methods in the system ?

Page 95: Pharo, an innovative and open-source Smalltalk

What is a dynamic language ?

Dynamic typing: greater polymorphism

Metaprogramming (metaclasses):allow language itself to be dynamically changed

allow hooks into object life cycle and method calls

Work with code as easily as dataClosures

Higher-order programming

Page 96: Pharo, an innovative and open-source Smalltalk

Readability

Shorter code is easier to read and maintain and refactor

Need balance between cryptic and expressive

Page 97: Pharo, an innovative and open-source Smalltalk

square^self*self

public int square(int x) { return x * x;}

Page 98: Pharo, an innovative and open-source Smalltalk

button on: #mouseDown send: #value to: [Object inform: 'You clicked me'].

button.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { (Button) view.setText(""You Clicked Me!") }});

Page 99: Pharo, an innovative and open-source Smalltalk

Pharo, an open-source Smalltalk

3

Page 100: Pharo, an innovative and open-source Smalltalk
Page 101: Pharo, an innovative and open-source Smalltalk

In a nutshell

Pharo = language + IDE

Pure object-oriented programming language (Smalltalk)

Dynamically language and trait-based

Open and flexible environment

Used Monticello for versionning (Metacello planned for 1.1)

Page 102: Pharo, an innovative and open-source Smalltalk
Page 103: Pharo, an innovative and open-source Smalltalk

iPhone

Page 104: Pharo, an innovative and open-source Smalltalk

Pharo class Browser

Page 105: Pharo, an innovative and open-source Smalltalk

other Pharo tools

Page 106: Pharo, an innovative and open-source Smalltalk
Page 107: Pharo, an innovative and open-source Smalltalk

Polymorph UI

Polymorph provides support for selectable UI themes in Morphic, extra widgets to support a consistent look&fell, a framework for easily creating UI in code.

Standard UI in Pharo

Page 108: Pharo, an innovative and open-source Smalltalk
Page 109: Pharo, an innovative and open-source Smalltalk

Demo of Pharo

Simple examples

BlinkMorph example

Skins support

MultiMorph UI (UITheme examples)

Page 110: Pharo, an innovative and open-source Smalltalk

Tests !

9179 unit tests includes in Pharo 1.0

9144 passes

20 expected failures

15 failures

0 errors

Page 111: Pharo, an innovative and open-source Smalltalk

Everybody can help

Reporting bugsConfirming bugsWriting testsWriting examplesWriting commentsSimple contributing fixesDeep discussion...

Page 112: Pharo, an innovative and open-source Smalltalk

FIX/ENHANCEMENTIn PharoInbox or

Changesets

Discussed on Mailing-

list

BUG Tracker

Integrated Rejected

BUG

Discussed on

Discussed on

Described

Described

Other version

Community Process

Page 113: Pharo, an innovative and open-source Smalltalk

Pharo by example vol. 2 on preparation

http://www.pharobyexample.org/

Page 114: Pharo, an innovative and open-source Smalltalk

Pharo SprintsMay 2008 Bern (Switzerland)July 2009 Bern (Switzerland)October 2009 Lille (France)November 2009 Buenos Aires (Argentina)

Page 115: Pharo, an innovative and open-source Smalltalk

ThanksHans Beck

Alexandre Bergel Cédric Beler

Torsten Bergmann Matthias Berth Ralph Boland

Noury Bouraqadi Brian Brown

Gwenael Casaccio Damien Cassou Nicolas Cellier Gary Chambers

Miguel Coba Gabriel Cotelli Carlos Crosetti Cyrille Delaunay Simon Denier

Marcus Denker Ramiro Diaz Trepat Stéphane Ducasse

Morales Durand Hernan Stephan Eggermont

Luc Fabresse

Matthew Fulmer Hilaire Fernandes

Julian Fitzell Tudor Girba Sean Glazier

Norbert Hartl Dale Henrichs Reinout Heeck

Eric Hochmeister Keith Hodges

Henrik Sperre Johansen Pavel Krivanek Adrian Kuhn

Adrian Lienhard Andreas Leidig

Mariano Martinez Peck Dave Mason

John McIntosh Johnaton Meichtry

Eliot Miranda Hernan Morales Durand

Philipp Marshall Jannick Menanteau

Yann Monclair Oscar Nierstrasz

David J Pennell Joseph Pelrine Alain Plantec Damien Pollet Lukas Renggli Jorge Ressia

Mike Roberts Robert Rothwell

David Rotlisberger Michael Rueger

Bill Schwab Niko Schwarz Igor Stasenko

Francois Stephany Serge Stinckwich

Mathieu Suen Lawrence Trutter Andrew Tween

Martin von loewis Juan Vuletich Steven Wirts

Hernan Wilkinson

Page 116: Pharo, an innovative and open-source Smalltalk

Join Us!

Creating good energy, software quality,learning and having fun

http://pharo-project.org

Page 117: Pharo, an innovative and open-source Smalltalk

Cộng đồng Smalltalk Việt

Smalltalk-VN mailing-list :

http://lists.squeakfoundation.org/mailman/listinfo/smalltalk-vn

Smalltalk flyer in vietnamese

EToys in vietnamese

Page 118: Pharo, an innovative and open-source Smalltalk

Occam's razor: "entities should not be multiplied beyond what is necessary" (entia non sunt multiplicanda praeter necessitatem)

the simplest solution is usually the correct one.

Page 119: Pharo, an innovative and open-source Smalltalk

Thank you for your attention.

Cám ơn sự quan tâm của bạn.