Pharo: Syntax in a Nutshell
-
Upload
marcus-denker -
Category
Technology
-
view
1.199 -
download
1
description
Transcript of Pharo: Syntax in a Nutshell
![Page 1: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/1.jpg)
Pharo: Syntax in a NutshellS. Ducasse and M. Denker http://www.pharo-project.org
![Page 2: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/2.jpg)
No constructors
No types declaration
No interfaces
No packages/private/protected
No parametrized types
No boxing/unboxing
And really powerful
Less is better
![Page 3: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/3.jpg)
Objects are instances of Classes
![Page 4: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/4.jpg)
Objects are instances of Classes
(10@200)
![Page 5: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/5.jpg)
Objects are instances of Classes
(10@200) class
![Page 6: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/6.jpg)
Objects are instances of Classes
(10@200) class
Point
![Page 7: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/7.jpg)
Classes are objects too
![Page 8: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/8.jpg)
Classes are objects too
Point selectors
![Page 9: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/9.jpg)
Classes are objects too
Point selectors
> an IdentitySet(#eightNeighbors #+ #isZero #sortsBefore: #degrees #printOn: #sideOf: #fourNeighbors #hash #roundUpTo: #min: #min:max: #max #adaptToCollection:andSend: #quadrantOf:
![Page 10: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/10.jpg)
Classes are objects too
Point instVarNames
![Page 11: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/11.jpg)
Classes are objects too
Point instVarNames
>#('x' 'y')
![Page 12: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/12.jpg)
Methods are public
![Page 13: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/13.jpg)
Instance variables are protected
![Page 14: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/14.jpg)
Single Inheritance
![Page 15: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/15.jpg)
Single Inheritance
Object subclass: #Point
instanceVariableNames: 'x y'
classVariableNames: ''
category: 'Graphics-Primitives'
![Page 16: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/16.jpg)
Complete Syntax on a PostCardexampleWithNumber: 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
![Page 17: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/17.jpg)
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
![Page 18: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/18.jpg)
Syntaxcomment: “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
![Page 19: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/19.jpg)
3 kinds of messagesUnary messages
Binary messages
Keywords messages
5 factorialTranscript cr
3 + 4
3 raisedTo: 10 modulo: 5
Transcript show: 'hello world'
![Page 20: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/20.jpg)
<= aPoint ! "Answer whether the receiver is neither! below nor to the right of aPoint."
! ^ x <= aPoint x and: [y <= aPoint y]
A typical method in PointMethod name Argument Comment
Return Binary messageKeyword messageInstance variable
Block
(2@3) <= (5@6) true
![Page 21: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/21.jpg)
• Anonymous method
• Passed as method argument or stored• Functions fct(x)= x*x+3, fct(2).
fct :=[:x| x * x + 3]. fct value: 2
Blocks
![Page 22: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/22.jpg)
Integer>>factorial | tmp |
tmp := 1. 2 to: self do: [:i| tmp := tmp * i]
#(1 2 3) do: [:each | each crLog]
Block usage
![Page 23: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/23.jpg)
| p pen |p := [email protected] := Pen new.pen up.pen goto: p; down; goto: p+p
Statements and cascades
Temporary variablesStatement
Cascade
![Page 24: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/24.jpg)
Control structuresEvery control structure is realized by message sends
4 timesRepeat: [Beeper beep]
max: aNumber! ^ self < aNumber ! ! ifTrue: [aNumber] ! ! ifFalse: [self]
![Page 25: Pharo: Syntax in a Nutshell](https://reader034.fdocuments.in/reader034/viewer/2022052619/5554f7c7b4c90566278b54e8/html5/thumbnails/25.jpg)
Simple and elegant