Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Pharo, an innovative and open-source Smalltalk

3,865 views

Published on

Published in: Education
  • As someone who used Smalltalk/V and VisualAge in the 1990s, I've been away from the language for too long. This presentation brings it all back. I'm sure it is equally useful for newcomers.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Pharo, an innovative and open-source Smalltalk

  1. 1. A clean, innovative, open-source Smalltalk http://www.pharo-project.org/ Serge Stinckwich Serge.Stinckwich@esug.org
  2. 2. 1 Smalltalk Syntax 2 Smalltalk object model 3 Pharo, an open-source Smalltalk
  3. 3. 1 Smalltalk syntax
  4. 4. What are the keywords ? How do you build expressions ?
  5. 5. Almost no keywords
  6. 6. Syntax for litterals 1 -10 30 3.14156 1e-10 -1.4e4 $A $z ‘Hello World’ #(1 2 3 4 5)
  7. 7. “A comment is a sequence of characters surrounded by quotation marks.”
  8. 8. Assignement x := 3. y := ‘Hello world’. z := x+4.
  9. 9. 6 pseudo-variables nil, true, false self, super, thisContext
  10. 10. Everything happens by message sending
  11. 11. (1) Unary message
  12. 12. receiver message.
  13. 13. 20 factorial
  14. 14. 2432902008176640000
  15. 15. ‘Esope reste et se repose’ reversed
  16. 16. ‘esoper es te etser eposE’
  17. 17. ‘12345’ isAllDigits
  18. 18. true
  19. 19. ‘Quelle est ma longueur ?’ size
  20. 20. 23
  21. 21. ‘foobar’ first
  22. 22. $f
  23. 23. (2) Binary Messages
  24. 24. aReceiver aSelector anArgument
  25. 25. 3+4
  26. 26. 7
  27. 27. ‘Bonjour ’, ‘monde’
  28. 28. ‘Bonjour monde’
  29. 29. 2@3
  30. 30. 2@3
  31. 31. (3) Keywords Messages
  32. 32. 12 between: 8 and:15.
  33. 33. 8 < 12 < 15 ?
  34. 34. true
  35. 35. #(1 4 9 16 25) at: 3
  36. 36. 9
  37. 37. aString.substring(2, 5) aString copyFrom: 2 to: 5 ColorValue hue: a new ColorValue(a, b, c) saturation: b value: c DateAndTime year: a day: new DateAndTime(a, b, c) b timeZone: c Easier to document the semantic role of arguments with keywords messages.
  38. 38. (Msg) > Unary > Binary > Keywords
  39. 39. Messages cascading
  40. 40. myBirthday := Date new. myBirthDay setYear: 1967. myBirthDay setMonth: 5. myBirthDay setDayOfMonth: 10.
  41. 41. myBirthday := Date new; setYear: 1967; setMonth: 5; setDayOfMonth: 10.
  42. 42. The End
  43. 43. if ... then ... else ?
  44. 44. Weather today isRaining ifTrue: [self takeMyUmbrella] ifFalse: [self takeMySunglasses] ifTrue:ifFalse is sent to an objet: a boolean
  45. 45. Closures [3+4] [‘Bonjour’ size. 3+4. #(1 2 3 4) at:2]
  46. 46. 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.
  47. 47. [1+2] value. ⇒ 3
  48. 48. Block could be parametrized [:param1 :param2 ... | statements]
  49. 49. [:x | x+1] value:2. [:x :y | x,y] value:’Bonjour’ value:’monde !’.
  50. 50. 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.
  51. 51. Dynamic binding enables polymorphism
  52. 52. Where is implemented ifTrue:ifFalse: ?
  53. 53. Where is implemented ifTrue:ifFalse: ? True>>ifTrue:aBlock1 ifFalse: aBlock2 ^aBlock1 value False>>ifTrue:aBlock1 ifFalse: aBlock2 ^aBlock2 value
  54. 54. Build your own control structure ! 7 ifSeven:[Object inform: ‘Its seven’] Number>>ifSeven:aBlock self=7 ifTrue:[^aBlock value]
  55. 55. Iterations
  56. 56. [Weather today isRaining] whileTrue:[self doNotGoOutside. self readAGoodBook]
  57. 57. Array | life | life := #(calvin hates suzie). life at:2 put:#loves. life. ⇒ #(#calvin #loves #suzie) life first. ⇒ #calvin life last. ⇒#suzie life indexOf:#calvin. ⇒ 1 life indexOf:#asterix ifAbsent:[Transcript show:’Je ne connais pas’].
  58. 58. Set s := Set new. s add:1. s add:2. s add:2. s. ⇒ a Set(1 2)
  59. 59. Interval -10 to:10. ⇒ (-10 to: 10) (-10 to:10) at:2. ⇒ -9 (-10 to:10) at:2 put:3. ⇒ erreur
  60. 60. 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')
  61. 61. OrderedCollection distributions 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')
  62. 62. 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:
  63. 63. Enumeration messages are polymorphic = they work whatever the collection is
  64. 64. 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]
  65. 65. 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)
  66. 66. 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.
  67. 67. 2 Smalltalk object model
  68. 68. Classe Rectangle Rectangle width height area ...
  69. 69. Object subclass: #Rectangle instanceVariableNames: 'width height' classVariableNames: '' poolDictionaries: '' category: 'Exemples-Heritage'
  70. 70. Rectangle>>width: w width := w Rectangle>>height: h height := h Rectangle>>width ^width Rectangle>>height ^height Rectangle>>area ^ width*height
  71. 71. Classe ColoredRectangle ColoredRectangle width height color area ...
  72. 72. A colored rectangle is like a rectangle but with a color ...
  73. 73. Operations that can be done a rectangle, can also be done on a colored rectangle (e.g surface calculus)
  74. 74. a ColoredRectangle isa Rectangle
  75. 75. Rectangle subclass: #ColoredRectangle instanceVariableNames: 'color' classVariableNames: '' poolDictionaries: '' category: 'Exemples-Heritage'
  76. 76. ColoredRectangle>>color ^color ColoredRectangle>>color: aColor color := aColor
  77. 77. Class inheritance Rectangle width height area ... ColoredRectangle color ...
  78. 78. a := Rectangle new. a width:10. a height:50. a width. ⇒ 10 a height. ⇒ 50 a area. ⇒ 500
  79. 79. b := ColoredRectangle new. b width:10. b height:50. b color: Color blue. b width. ⇒ 10 b height. ⇒ 50 b color. ⇒ Color blue b area. ⇒ 500
  80. 80. Rule 1 Everything is an object Rule 2 Every object is an instance of one class Rule 3 Every class has a super-class Rule 4 Everything happens by message sending
  81. 81. Classes are also objects ! 1 class⇒ SmallInteger 20 factorial class ⇒ LargePositiveInteger ‘Hello’ class ⇒ ByteString #(1 2 3) class ⇒ Array
  82. 82. Every class has a superclass Integer superclass ⇒ Integer Number superclass ⇒ Magnitude Magnitude superclass ⇒ Object
  83. 83. doesNotUnderstand:
  84. 84. If a class is an object, every class should be also an instance of a specific classe. Metaclasses !
  85. 85. Metaclasses hierarchy is parallel to class hierarchy
  86. 86. Every metaclasses is instance of Metaclass
  87. 87. The metaclass of Metaclass is an instance of Metaclass
  88. 88. How many classes in the system ? Object allSubclasses size
  89. 89. How many abstract methods in the system ? Object allSubclasses do: [:aClass| aClass methodDict keys select: [:aMethod | (aClass>>aMethod) isAbstract ]]
  90. 90. 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 data Closures Higher-order programming
  91. 91. Readability Shorter code is easier to read and maintain and refactor Need balance between cryptic and expressive
  92. 92. square ^self*self public int square(int x) { return x * x; }
  93. 93. 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!") } });
  94. 94. 3 Pharo, an open-source Smalltalk
  95. 95. 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)
  96. 96. iPhone
  97. 97. Pharo class Browser
  98. 98. other Pharo tools
  99. 99. 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
  100. 100. Demo of Pharo Simple examples BlinkMorph example Skins support MultiMorph UI (UITheme examples)
  101. 101. Tests ! 9179 unit tests includes in Pharo 1.0 9144 passes 20 expected failures 15 failures 0 errors
  102. 102. Everybody can help Reporting bugs Confirming bugs Writing tests Writing examples Writing comments Simple contributing fixes Deep discussion...
  103. 103. Community Process FIX/ ENHANCEMENT Discussed on In PharoInbox or Discussed Changesets on Mailing- BUG list Described Described BUG Tracker Discussed on Other version Integrated Rejected
  104. 104. http://www.pharobyexample.org/ Pharo by example vol. 2 on preparation
  105. 105. Pharo Sprints May 2008 Bern (Switzerland) July 2009 Bern (Switzerland) October 2009 Lille (France) November 2009 Buenos Aires (Argentina)
  106. 106. Thanks Hans Beck Matthew Fulmer Hilaire Fernandes Julian Fitzell David J Pennell Alexandre Bergel Tudor Girba Joseph Pelrine Cédric Beler Sean Glazier Alain Plantec Torsten Bergmann Norbert Hartl Damien Pollet Matthias Berth Dale Henrichs Lukas Renggli Ralph Boland Reinout Heeck Jorge Ressia Noury Bouraqadi Eric Hochmeister Mike Roberts Brian Brown Keith Hodges Robert Rothwell Gwenael Casaccio Henrik Sperre Johansen David Rotlisberger Damien Cassou Pavel Krivanek Michael Rueger Nicolas Cellier Adrian Kuhn Bill Schwab Gary Chambers Adrian Lienhard Niko Schwarz Miguel Coba Andreas Leidig Igor Stasenko Gabriel Cotelli Mariano Martinez Peck Francois Stephany Carlos Crosetti Dave Mason Serge Stinckwich Cyrille Delaunay John McIntosh Mathieu Suen Simon Denier Johnaton Meichtry Lawrence Trutter Marcus Denker Eliot Miranda Andrew Tween Ramiro Diaz Trepat Hernan Morales Durand Martin von loewis Stéphane Ducasse Philipp Marshall Juan Vuletich Morales Durand Hernan Jannick Menanteau Steven Wirts Stephan Eggermont Yann Monclair Hernan Wilkinson Luc Fabresse Oscar Nierstrasz
  107. 107. Join Us! Creating good energy, software quality, learning and having fun http://pharo-project.org
  108. 108. 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
  109. 109. 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.
  110. 110. Thank you for your attention. Cám ơn sự quan tâm của bạn.

×