Pharo, an innovative and open-source Smalltalk

Preview:

Citation preview

http://www.pharo-project.org/

A clean, innovative, open-source Smalltalk

Serge StinckwichSerge.Stinckwich@esug.org

1 Smalltalk Syntax

Smalltalk object model

Pharo, an open-source Smalltalk

2

3

Smalltalk syntax1

What are the keywords ?

How do you build expressions ?

Almost no keywords

Syntax for litterals

1 -10 30

3.14156 1e-10 -1.4e4

$A $z

‘Hello World’

#(1 2 3 4 5)

“A comment is a sequence of characters surrounded by

quotation marks.”

Assignement

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

6 pseudo-variables

nil, true, false

self, super, thisContext

Everything happensby message sending

(1) Unary message

receiver message.

20 factorial

2432902008176640000

‘Esope reste et se repose’reversed

‘esoper es te etser eposE’

‘12345’ isAllDigits

true

‘Quelle est ma longueur ?’ size

23

‘foobar’ first

$f

(2) Binary Messages

aReceiver aSelector anArgument

3 + 4

7

‘Bonjour ’, ‘monde’

‘Bonjour monde’

2@3

2@3

(3) Keywords Messages

12 between: 8 and:15.

8 < 12 < 15 ?

true

#(1 4 9 16 25) at: 3

9

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.

(Msg) > Unary > Binary > Keywords

Messages cascading

myBirthday := Date new.

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

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

TheEnd

if ... then ... else ?

Weather today isRaining

ifTrue: [self takeMyUmbrella]

ifFalse: [self takeMySunglasses]

ifTrue:ifFalse is sent to an objet: a boolean

Closures

[3+4]

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

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.

[1+2] value. ⇒ 3

Block could be parametrized

[:param1 :param2 ... | statements]

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

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

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.

Dynamic binding enables polymorphism

Where is implemented ifTrue:ifFalse: ?

Where is implemented ifTrue:ifFalse: ?

True>>ifTrue:aBlock1 ifFalse: aBlock2

^aBlock1 value

False>>ifTrue:aBlock1 ifFalse: aBlock2

^aBlock2 value

Build your own control structure !

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

Number>>ifSeven:aBlock

self=7 ifTrue:[^aBlock value]

Iterations

[Weather today isRaining]whileTrue:[self doNotGoOutside.

self readAGoodBook]

| 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

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

Set

Interval

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

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')

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')

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:

Enumeration messages are polymorphic=

they work whatever the collection is

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]

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)

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.

Smalltalk object model2

Classe Rectangle

Rectangle

widthheight

area...

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

Rectangle>>width: wwidth := w

Rectangle>>height: hheight := h

Rectangle>>width^width

Rectangle>>height^height

Rectangle>>area^ width*height

Classe ColoredRectangle

ColoredRectangle

widthheightcolor

area...

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

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

(e.g surface calculus)

a ColoredRectangle isa Rectangle

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

ColoredRectangle>>color ^color

ColoredRectangle>>color: aColor color := aColor

Class inheritanceRectangle

widthheight

area...

ColoredRectangle

color

...

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

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

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

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

Classes are also objects !

1 class⇒ SmallInteger

20 factorial class ⇒ LargePositiveInteger‘Hello’ class ⇒ ByteString

#(1 2 3) class ⇒ Array

Every class has a superclass

Integer superclass ⇒ Integer

Number superclass ⇒ Magnitude

Magnitude superclass ⇒ Object

doesNotUnderstand:

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

instance of a specific classe.

Metaclasses !

Metaclasses hierarchy is parallel to class hierarchy

Every metaclasses is instance of Metaclass

The metaclass of Metaclass is an

instance of Metaclass

How many classes in the system ?

Object allSubclasses size

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

How many abstract methods in the system ?

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

Readability

Shorter code is easier to read and maintain and refactor

Need balance between cryptic and expressive

square^self*self

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

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!") }});

Pharo, an open-source Smalltalk

3

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)

iPhone

Pharo class Browser

other Pharo tools

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

Demo of Pharo

Simple examples

BlinkMorph example

Skins support

MultiMorph UI (UITheme examples)

Tests !

9179 unit tests includes in Pharo 1.0

9144 passes

20 expected failures

15 failures

0 errors

Everybody can help

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

FIX/ENHANCEMENTIn PharoInbox or

Changesets

Discussed on Mailing-

list

BUG Tracker

Integrated Rejected

BUG

Discussed on

Discussed on

Described

Described

Other version

Community Process

Pharo by example vol. 2 on preparation

http://www.pharobyexample.org/

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

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

Join Us!

Creating good energy, software quality,learning and having fun

http://pharo-project.org

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

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.

Thank you for your attention.

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

Recommended