Pharocheatsheet.Pdf2018-03-14 08:22554 KB
Total Page:16
File Type:pdf, Size:1020Kb
Files AND StrEAMS • The METHODS PANE LISTS THE METHODS OF THE SELECTED PRotocol; ICONS ARE CLICKABLE AND TRIGGER SPECIAL actions; WORK := FileSystem DISK workingDirECTORY. • The SOURCE CODE PANE SHOWS THE SOURCE CODE OF THE SELECTED STREAM := (work / ’foo.txt’) writeStream. method. STREAM NExtPutAll: ’Hello WORLD’. STREAM close. DefiNING A CLASS STREAM := (work / ’foo.txt’) ReadStream. TO ADD A CLASS OR EDIT A class, EDIT THE PROPOSED template! STREAM contents. ’Hello WORLD’ An INNOvative, open-sourCE Smalltalk-inspirED The FOLLOWING EXPRESSION DEfiNES THE CLASS Counter AS A sub- STREAM close. CLASS OF Object. IT DEfiNES TWO INSTANCE VARIABLES count AND LANGUAGE AND SYSTEM FOR LIVE PROGRAMMING initialValue INSIDE THE PACKAGE MyCounter. Pharo: A Live PrOGRAMMING EnvirONMENT Object subclass: #Counter http://www.pharo.orG instanceVariableNames: ’count initialValue’ PharO COMES WITH AN INTEGRATED DEVELOPMENT ENVIRonment. classVariableNames: ’’ PharO IS BOTH AN object-oriented, dynamically-typed GENERal- PharO IS A LIVE PROGRAMMING ENVIRONMENT: YOU CAN MODIFY YOUR package: ’MyCounter’ PURPOSE LANGUAGE AND ITS OWN PROGRAMMING ENVIRonment. The OBJECTS AND YOUR CODE WHILE YOUR PROGRAM IS EXecuting. All The METHOD initialize IS AUTOMATICALLY INVOKED WHEN A NEW LANGUAGE HAS A SIMPLE AND EXPRESSIVE SYNTAX WHICH CAN BE PharO TOOLS ARE IMPLEMENTED IN Pharo: INSTANCE IS CREATED BY SENDING THE MESSAGE new TO THE CLASS i.e., LEARNED IN A FEW minutes. Concepts IN PharO ARE VERY CONSISTENT: Counter new . • Everything IS AN object: buttons, colors, ARRays, numbers, • A CODE BROWSER WITH Refactorings; Counter >> initialize • A debugger, A workspace, AND inspectors; super initialize. classes, methods. Everything! • THE COMPILER ITSELF AND much, MUCH MORe. count := 0. • A SMALL NUMBER OF rules, NO EXceptions! Code CAN BE INSPECTED AND EVALUATED DIRECTLY IN THE image, Counter >> initialize IS A NOTATION TO INDICATE THAT THE Main WEB Sites USING SIMPLE KEY COMBINATIONS AND MENUS (open THE CONTEXTUAL FOLLOWING TEXT IS THE CONTENT OF THE METHOD initialize IN THE MENU ON ANY SELECTED TEXT TO SEE AVAILABLE options). CLASS Counter. Code HOSTING http://smalltalkhub.com Questions http://discord.gg/Sj2rhxn Methods Blog http://pharOWEEKLY.worDPRess.com The 5 Panes PharO Code BrOWSER Methods ARE PUBLIC AND virtual. TheY ARE ALWAYS LOOKED UP IN THE Contributors http://pharo.org/about CLASS OF THE Receiver. By DEFAULT A METHOD RETURNS self. Class TOPICS http://topics.pharo.orG METHODS FOLLOW THE SAME DYNAMIC LOOKUP AS INSTANCE methods. Consortium http://consortium.pharo.orG Methods factorial Packages Classes Protocols Method DEfiNED IN CLASS Integer. Association http://association.pharo.orG Integer >> factorial "Answer the factorial of the receiver." PharoBooks self = 0 ifTrue: [^ 1]. PharO BOOKS ARE AVAILABLE at: http://books.pharo.orG self > 0 ifTrue: [^ self * (self - 1) factorial]. PharO By Example, Deep INTO Pharo, Enterprise Pharo: A WEB Unit TESTING Perspective, Numerical Methods IN Pharo, TinyBlog Tutorial, Code pane Dynamic WEB DeVELOPMENT IN Seaside (http://book.seaside.st) A TEST MUST BE IMPLEMENTED IN A METHOD WHOSE NAME STARTS WITH test AND IN A CLASS THAT INHERITS FROM TestCase. MorE BOOKS http://stephane.ducasse.free.fr/FreeBooks OrderedCollectionTest >> testAdd | added| Minimal Syntax • The PACKAGES PANE SHOWS ALL THE PACKAGES OF THE system. added := collection add: ’foo’. • The CLASSES PANE SHOWS THE CLASS HIERARCHY OF THE SELECTED self assert: (collection includes: ’foo’). Six RESERVED WORDS ONLY package; THE CLASS SIDE CHECKBOX ALLOWS FOR GETTING THE meth- The SECOND LINE DECLARES THE VARIABLE added. The MESSAGE nil THE UNDEfiNED OBJECT ODS OF THE metaclass. assert: EXPECTS A TRUE value. true,false THE BOOLEAN OBJECTS • The PROTOCOLS PANE GROUPS THE METHODS OF THE SELECTED CLASS self THE RECEIVER OF THE CURRENT MESSAGE TO EASE navigation. When A PROTOCOL NAME STARTS WITH A *, super THE RECEIVER BUT FOR ACCESSING OVERRIDDEN METHODS OF THIS PROTOCOL BELONG TO A DIffERENT PACKAGE (e.g., A simple, UNIFORM AND POWERFUL MODEL METHODS THE *Fuel PROTOCOL GROUPS METHODS THAT BELONG TO THE Fuel PharO HAS A SIMPLE dynamically-typed OBJECT model: thisContext THE CURRENT METHOD OR BLOCK ACTIVATION package); ..................................................................................................................................................................... ..................................................................................................................................................................... ..................................................................................................................................................................... 2=2 Minimal Syntax (II) 3+4 7 FROM ) WITH A BLOCK AS ARgument. Because THE BOOLEAN IS true ’Hello’ , ’ WORLD’ ’Hello WORLD’ , THE BLOCK IS EXECUTED AND AN EXCEPTION IS signaled. Object CONSTRUCTORS & RESERVED SYNTACTIC CONSTRUCTS #(’Hello World’ $!) + 3 4 "COMMENT" The MESSAGE IS SENT TO THE OBJECT WITH AS ARgument. do: [ :e| Transcript show:e] ’Hello’ , ’STRING’ SEQUENCE OF CHARACTERS The STRING RECEIVES THE MESSAGE (comma) WITH do: ’ World’ Send THE MESSAGE TO AN ARRAY. This EXECUTES THE BLOCK #symbol UNIQUE STRING AS THE ARgument. ONCE FOR EACH element, PASSING IT VIA THE e PARameter. As A $a, CharACTER SPACE TWO WAYS TO CREATE CHARACTERS A KEYWORD MESSAGE CAN TAKE ONE OR MORE ARGUMENTS THAT ARE Result, Hello World! IS printed. 12 2r1100 16rC TWELVE (decimal, BINARY, HExa) INSERTED IN THE MESSAGE name. 3.14 1.2e3 floating-point NUMBERS Common Constructs #(abc 123) LITERAL ARRAY WITH THE SYMBOL ’PharO’ allButFirst:2. ’ARO’ #abc AND THE NUMBER 123 [ :x | X + 2 ] value: 7 9 Conditionals {foo.3+2} DYNAMIC ARRAY BUILT FROM 2 Ex- 3 to: 10 By:2. (3 to: 10 By: 2) CONDITION IF (condition) PRESSIONS The SECOND LINE EXECUTES A block. The THIRD EXAMPLE SENDS ifTrue: [ ACTION ] { action(); } #[123 21 255] BYTE ARRAY to:by: TO 3, WITH ARGUMENTS 10 AND 2; THIS RETURNS AN INTERVAL ifFalse: [ anotherAction ] ELSE { anotherAction(); } | foo bar| DECLARATION OF TWO TEMPORARY CONTAINING 3, 5, 7, AND 9. [ CONDITION ] whileTrue: WHILE (condition) { action(); VARIABLES Message PrECEDENCE [ action. anotherAction ] anotherAction(); } var := expr ASSIGNMENT exp1. exp2 > > > PERIOD - STATEMENT SEPARATOR ParENTHESES UNARY BINARY KEYWORd, AND fiNALLY FROM LEFT Loops/ITERATORS ; SEMICOLON - MESSAGE CASCADE TO right. 1 to: 11 do: [ :i | for(int i=1; i<11; i++){ [:p| EXPR ] CODE BLOCK WITH A PARAMETER (15 between:1 and:2+4*3) NOT FALSE TRANSCRIPT SHOw: I ; CR ] System.out.println(i); } <unary> METHOD ANNOTATION <key:’any’ wrd:#lit > WITH ANY LITERAL ARGUMENTS Messages + AND * ARE SENT first, THEN between:and: IS sent, | NAMES | String [] NAMES ={"A", "B", "C"}; ^ expr CARET - Return/answer A RESULT AND not. The RULE SUffERS NO EXception: OPERATORS ARE JUST NAMES := #(’A’’B’’C’). for( String NAME : NAMES ) { FROM A METHOD BINARY MESSAGES WITH NO NOTION OF MATHEMATICAL PRECEDENCE. NAMES do: [ :each | System.out.print( NAME ); 2+4*3 READS left-to-right AND GIVES 18, NOT 14! TRANSCRIPT SHOw: each, ’ , ’ ] System.out.print(","); } Message Sending aCol at:i Cascade: Sending Muliple Messages TO THE Same Object Collections START AT 1. ACCESSES ELEMENT AT I AND aCol at:i put:value i value When WE SEND A MESSAGE TO AN OBJECT (the RECEIVER), THE cor- SETS ELEMENT AT TO . Using ; (a cascade) MULTIPLE MESSAGES ARE SENT TO THE RESULT OF RESPONDING METHOD IS SELECTED AND EXecuted, AND THE METHOD THE SAME EXPRession. HerE ; ARRIVES AFTER Add: 1, SO MESSAGES Collections ANSWERS AN object. Message SYNTAX MIMICS NATURAL languages, add: 2 AND add: 3 ARE SENT TO add: 1’S Receiver: A collection. WITH A subject, A verb, AND complements. OrderedCollection new #(4 2 1) at: 3 1 add:1; #(4 2 1) COPY at: 3 put: 6 #(4 2 6) PharO Java add:2; {4 . 2 . 1} at: 3 put: 6 #(4 2 6) add:3. aColor r: 0.2 g: 0.3 b: 0 aColor.setRGB(0.2,0.3,0) (ArrAY NEw: 2) add: 4; add: 2 ; YOURSELF #(4 2) D at: ’1’ put: ’Chocolate’. d.put("1", "Chocolate"); The WHOLE MESSAGE CASCADE VALUE IS THE VALUE OF THE LAST mes- Set NEW add: 4; add: 4 ; YOURSELF aSet(4) SAGE SENT (HERE 3). TO RETURN THE RECEIVER OF THE MESSAGE cas- Dictionary NEW ThrEE TYPES OF Messages: Unary, Binary, AND KeYWORD yourself CADE INSTEAD (i.e., THE collection), SEND AS THE LAST at: #a put: ’Alpha’ ; YOURSELF A Dictionary(#a- A UNARY MESSAGE HAS NO ARguments. MESSAGE OF THE cascade. >’Alpha’) Blocks ArrAY NEW. anArrAY #(4 2 1) size. 3 Blocks ARE OBJECTS CONTAINING CODE THAT IS EXECUTED ON demand. new TheY ARE THE BASIS FOR CONTROL STRUCTURes: CONDITIONALS & loops. IS AN UNARY MESSAGE SENT TO CLASSES (classes ARE objects). 2=2 A BINARY MESSAGE TAKES ONLY ONE ARGUMENT AND IS NAMED BY ifTrue: [ Error signal: ’Help’]. + - * = < > ONE OR MORE SYMBOL CHARACTERS FROM , , , , , , ... Send THE MESSAGE ifTrue: TO THE BOOLEAN true (computed ..................................................................................................................................................................... ..................................................................................................................................................................... ......................................................................................................................................................................