Pharo: Syntax in a Nutshell

Marcus Denker
Marcus DenkerResearch Scientist at INRIA
Pharo: Syntax in a
Nutshell
S. Ducasse and M. Denker
http://www.pharo-project.org
Less is better

 No constructors
 No types declaration
 No interfaces
 No packages/private/protected
 No parametrized types
 No boxing/unboxing
 And really powerful
Objects are instances of Classes
Objects are instances of Classes



(10@200)
Objects are instances of Classes



(10@200) class
Objects are instances of Classes



(10@200) class

Point
Classes are objects too
Classes are objects too



Point selectors
Classes are objects too



Point selectors


> an IdentitySet(#eightNeighbors #+ #isZero
#sortsBefore: #degrees #printOn: #sideOf:
#fourNeighbors #hash #roundUpTo: #min: #min:max:
#max #adaptToCollection:andSend: #quadrantOf:
Classes are objects too



Point instVarNames
Classes are objects too



Point instVarNames

>#('x' 'y')
Methods are public
Instance variables are protected
Single Inheritance
Single Inheritance
Object subclass: #Point

	   instanceVariableNames: 'x y'

	   classVariableNames: ''

	   category: 'Graphics-Primitives'
Complete Syntax on a PostCard
exampleWithNumber: x

“A method that has unary, binary, and key word messages, declares arguments and
temporaries (but not block temporaries), accesses a global variable (but not and
instance variable), uses literals (array, character, symbol, string, integer, float), uses the
pseudo variable true false, nil, self, and super, and has sequence, assignment, return
and cascade. It has both zero argument and one argument blocks.”

   	 |y|

     true & false not & (nil isNil) ifFalse: [self halt].

   	 y := self size + super size.

   
 #($a #a ‘a’ 1 1.0)

           do: [:each | Transcript show: (each class name); show: (each printString); show:
           ‘ ‘].

     ^x<y
Language Constructs
	^	    return

 “
    comments
#	     symbol or array
 ‘
    string
[ ]	   block or byte array
.	     separator and not terminator (or namespace access in VW)

;	     cascade (sending several messages to the same instance)
|	     local or block variable
Syntax
comment:
      “a comment”
character:	    $c $h $a $r $a $c $t $e $r $s $# $@
string:
       ‘a nice string’ ‘lulu’ ‘l’’idiot’
symbol:	       #mac #+
array:	        #(1 2 3 (1 3) $a 4)
byte array:	   #[1 2 3]

integer:	      1, 2r101
real:	         1.5, 6.03e-34,4, 2.4e7
float:          1/33
boolean:       true, false
point:	        10@120
3 kinds of messages
                  5 factorial
Unary messages    Transcript cr


Binary messages     3 + 4


Keywords messages
             3 raisedTo: 10 modulo: 5

             Transcript show: 'hello world'
A typical method in Point
Method name   Argument          Comment

   <= aPoint
   ! "Answer whether the receiver is neither
   ! below nor to the right of aPoint."

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

 Return      Binary message          Block
   Instance variable      Keyword message



                    (2@3) <= (5@6)           true
Blocks
• Anonymous method
• Passed as method argument or stored
• Functions
  	 	     fct(x)= x*x+3, fct(2).

  	 	     fct :=[:x| x * x + 3].
          fct value: 2
Block usage

  Integer>>factorial
     | tmp |
     tmp := 1.
     2 to: self do: [:i| tmp := tmp * i]


  #(1 2 3) do: [:each | each crLog]
Statements and cascades

    Temporary variables
                           Statement
        | p pen |
        p := 100@100.
        pen := Pen new.
        pen up.
        pen goto: p; down; goto: p+p


                          Cascade
Control structures
Every control structure is realized by message sends
      4 timesRepeat: [Beeper beep]




        max: aNumber
        ! ^ self < aNumber
        ! !   ifTrue: [aNumber]
        ! !   ifFalse: [self]
Simple and elegant
1 of 25

Recommended

Introduction To C#Introduction To C#
Introduction To C#SAMIR BHOGAYTA
5.1K views119 slides
Chapter 2Chapter 2
Chapter 2application developer
406 views23 slides
Python basicPython basic
Python basicSaifuddin Kaijar
12.2K views25 slides
Lecture 1Lecture 1
Lecture 1Soran University
716 views72 slides

More Related Content

What's hot(20)

 C# basics C# basics
C# basics
Dinesh kumar14.5K views
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
Pierre de Lacaze5.4K views
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
Dmitrii Stoian639 views
F# for C# devs - Copenhagen .Net 2015F# for C# devs - Copenhagen .Net 2015
F# for C# devs - Copenhagen .Net 2015
Phillip Trelford1K views
Python BasicsPython Basics
Python Basics
primeteacher321.4K views
java vs C#java vs C#
java vs C#
Shivalik college of engineering1.7K views
Hack programming languageHack programming language
Hack programming language
Radu Murzea408 views
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
Ni 11.5K views
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
Mike Bowler7.6K views
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
Sumit Satam4.3K views
Python basicsPython basics
Python basics
RANAALIMAJEEDRAJPUT2.5K views
Programming with PythonProgramming with Python
Programming with Python
Rasan Samarasinghe6.2K views
Python Session - 4Python Session - 4
Python Session - 4
AnirudhaGaikwad4377 views
Loops in PythonLoops in Python
Loops in Python
Arockia Abins484 views
Python advancePython advance
Python advance
Deepak Chandella149 views
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
Jaganadh Gopinadhan3.6K views

Similar to Pharo: Syntax in a Nutshell(20)

Pharo tutorial at ECOOP 2013Pharo tutorial at ECOOP 2013
Pharo tutorial at ECOOP 2013
Pharo20.9K views
Pharo tutorial at ECOOP 2013Pharo tutorial at ECOOP 2013
Pharo tutorial at ECOOP 2013
Damien Cassou1.3K views
Smalltalk  Bar Camp Hanoi 2009Smalltalk  Bar Camp Hanoi 2009
Smalltalk Bar Camp Hanoi 2009
Serge Stinckwich764 views
5 - OOP - Smalltalk in a Nutshell (c)5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)
The World of Smalltalk552 views
Pharo Hands-On: 02 syntaxPharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntax
Pharo1.3K views
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
Aleš Najmann452 views
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
AbhishekSharma295841 views
RakudoRakudo
Rakudo
awwaiid789 views
Python.pdfPython.pdf
Python.pdf
Shivakumar B N159 views
02 basics02 basics
02 basics
The World of Smalltalk1.3K views

More from Marcus Denker(20)

Soil And PharoSoil And Pharo
Soil And Pharo
Marcus Denker47 views
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11
Marcus Denker50 views
Demo: Improved DoItDemo: Improved DoIt
Demo: Improved DoIt
Marcus Denker4 views
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
Marcus Denker413 views
Variables in PharoVariables in Pharo
Variables in Pharo
Marcus Denker84 views
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for Pharo
Marcus Denker292 views
Slot CompositionSlot Composition
Slot Composition
Marcus Denker295 views
Lecture: Advanced Reflection. MetaLinksLecture: Advanced Reflection. MetaLinks
Lecture: Advanced Reflection. MetaLinks
Marcus Denker1.4K views
PHARO IOTPHARO IOT
PHARO IOT
Marcus Denker2.9K views
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite Game
Marcus Denker1.2K views
Lecture: MetaLinksLecture: MetaLinks
Lecture: MetaLinks
Marcus Denker4.1K views
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to Pharo
Marcus Denker684 views
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in Practice
Marcus Denker860 views
Pharo6 - ESUG17Pharo6 - ESUG17
Pharo6 - ESUG17
Marcus Denker709 views
Pharo6Pharo6
Pharo6
Marcus Denker503 views

Recently uploaded(20)

ISWC2023-McGuinnessTWC16x9FinalShort.pdfISWC2023-McGuinnessTWC16x9FinalShort.pdf
ISWC2023-McGuinnessTWC16x9FinalShort.pdf
Deborah McGuinness80 views
Green Leaf Consulting: Capabilities DeckGreen Leaf Consulting: Capabilities Deck
Green Leaf Consulting: Capabilities Deck
GreenLeafConsulting147 views
ThroughputThroughput
Throughput
Moisés Armani Ramírez25 views

Pharo: Syntax in a Nutshell

  • 1. Pharo: Syntax in a Nutshell S. Ducasse and M. Denker http://www.pharo-project.org
  • 2. Less is better No constructors No types declaration No interfaces No packages/private/protected No parametrized types No boxing/unboxing And really powerful
  • 4. Objects are instances of Classes (10@200)
  • 5. Objects are instances of Classes (10@200) class
  • 6. Objects are instances of Classes (10@200) class Point
  • 8. Classes are objects too Point selectors
  • 9. Classes are objects too Point selectors > an IdentitySet(#eightNeighbors #+ #isZero #sortsBefore: #degrees #printOn: #sideOf: #fourNeighbors #hash #roundUpTo: #min: #min:max: #max #adaptToCollection:andSend: #quadrantOf:
  • 10. Classes are objects too Point instVarNames
  • 11. Classes are objects too Point instVarNames >#('x' 'y')
  • 15. Single Inheritance Object subclass: #Point instanceVariableNames: 'x y' classVariableNames: '' category: 'Graphics-Primitives'
  • 16. Complete Syntax on a PostCard exampleWithNumber: x “A method that has unary, binary, and key word messages, declares arguments and temporaries (but not block temporaries), accesses a global variable (but not and instance variable), uses literals (array, character, symbol, string, integer, float), uses the pseudo variable true false, nil, self, and super, and has sequence, assignment, return and cascade. It has both zero argument and one argument blocks.” |y| true & false not & (nil isNil) ifFalse: [self halt]. y := self size + super size. #($a #a ‘a’ 1 1.0) do: [:each | Transcript show: (each class name); show: (each printString); show: ‘ ‘]. ^x<y
  • 17. Language Constructs ^ return “ comments # symbol or array ‘ string [ ] block or byte array . separator and not terminator (or namespace access in VW) ; cascade (sending several messages to the same instance) | local or block variable
  • 18. Syntax comment: “a comment” character: $c $h $a $r $a $c $t $e $r $s $# $@ string: ‘a nice string’ ‘lulu’ ‘l’’idiot’ symbol: #mac #+ array: #(1 2 3 (1 3) $a 4) byte array: #[1 2 3] integer: 1, 2r101 real: 1.5, 6.03e-34,4, 2.4e7 float: 1/33 boolean: true, false point: 10@120
  • 19. 3 kinds of messages 5 factorial Unary messages Transcript cr Binary messages 3 + 4 Keywords messages 3 raisedTo: 10 modulo: 5 Transcript show: 'hello world'
  • 20. A typical method in Point Method name Argument Comment <= aPoint ! "Answer whether the receiver is neither ! below nor to the right of aPoint." ! ^ x <= aPoint x and: [y <= aPoint y] Return Binary message Block Instance variable Keyword message (2@3) <= (5@6) true
  • 21. Blocks • Anonymous method • Passed as method argument or stored • Functions fct(x)= x*x+3, fct(2). fct :=[:x| x * x + 3]. fct value: 2
  • 22. Block usage Integer>>factorial | tmp | tmp := 1. 2 to: self do: [:i| tmp := tmp * i] #(1 2 3) do: [:each | each crLog]
  • 23. Statements and cascades Temporary variables Statement | p pen | p := 100@100. pen := Pen new. pen up. pen goto: p; down; goto: p+p Cascade
  • 24. Control structures Every control structure is realized by message sends 4 timesRepeat: [Beeper beep] max: aNumber ! ^ self < aNumber ! ! ifTrue: [aNumber] ! ! ifFalse: [self]