31
Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Embed Size (px)

Citation preview

Page 1: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Smalltalk (and Squeak)

Aida Dungan and

Rick Shreve

Page 2: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Historical perspective:

Alan Kay developed Smalltalk as part of a complete computer system (language, computer, and programmingenvironment) at the Xerox Palo Alto Research Center(PARC) in the early 1970s. This system included suchcomputing novelties (then) as a mouse and a windowed environment.

Page 3: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

The first practical release of Smalltalk was Smalltalk-72, in 1972, by Dan Ingalls.There is no official standard for the language, but Smalltalk-80 is the generallyaccepted standard. Squeak is the programming environment we will be workingwith for our Smalltalk uses. From the www.squeak.org website: “Squeak is an open,highly-portable Smalltalk-80 implementation whose virtual machine is writtenentirely in Smalltalk, making it easy to debug, analyze, and change. To achievepractical performance, a translator produces an equivalent C program whoseperformance is comparable to commercial Smalltalks.” More on Squeak in a bit.

Page 4: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

The lack of practical implementations of Smalltalk used to be an impedance to itsgrowth, but it has a devoted following (there’s definitely a cross-over ofinformation between the Smalltalk developer superset, and the Squeakdeveloper subset) which has ported implementations to just about any operatingsystem you can think of; oh and it’s free.

Page 5: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

In addition to the unusual emergence circumstance for thislanguage, it was also unique in that object orientation was aprimitive built-in concept.

There is only a small core language definition. The main thing youdo when developing projects in Smalltalk is to develop classes as subclassesof other classes, and develop methods within these new classes.

Page 6: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

So, back to Squeak…It runs bit-identical on many platforms (Windows, Mac, Unix, and others).

Squeak!

The primary design team is at Disney, but again, it is open-source.

Page 7: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

What is Squeak?To quote from Dwight Hughes, a frequent contributor to the

Squeak mailing list, "How is Squeak important? Squeak

extends the fundamental Smalltalk philosophy of complete

openness -- where everything is available to see,

understand, modify, and extend for whatever purpose -- to

include even the VM. It is a genuine, complete, compact,

efficient Smalltalk-80 environment (*not* a toy). It is not

specialized for any particular hardware/OS platform. Porting

is easy -- you are not fighting entrenched platform/OS

dependencies to move to a new system or configuration. It

has essentially been put into the public domain - greatly

broadening potential interest, and potential applications. The

core team behind Squeak includes Dan Ingalls, Alan Kay,

Ted Kaehler, John Maloney, and Scott Wallace. All of this

has attracted many of the best and most experienced

Smalltalk programmers and implementers in the world."

Page 8: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

What’s legal?

Here is a paraphrase of the license terms:You are allowed to change Squeak, write extensions toSqueak, build an application in Squeak, and include some orall of Squeak with your products. You may distribute all ofthese things along with Squeak, or portions of Squeak, forfree or for money. However, you must distribute these thingsunder a license that protects Apple in the way described inthis license.If you modify any of the methods of class objects (or their relationships) thatcome with Squeak (as opposed to building on top of the classes in the release),you must post the modifications on a web site or otherwise make them availablefor free to others, just as has been done with Squeak. The same is true if youport Squeak to another machine or operating system - you must post your porton a web site or otherwise make it available for free to others under the samelicense terms.

Page 9: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Data objects:

Classes define types in Smalltalk. Variable naming conventions in Smalltalksuggest that strings of letters of multiple words be used. The first letter should belower-case (upper-case in a global variable), and successive words should beginwith a capital letter: myVariable, anInteger, etc… Strings are enclosed by singlequotes.

Smalltalk is an interpreted language, so execution is dynamic; variable types arenot specified.

Page 10: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

There are no built-in structured data in Smalltalk. Data objects such as arrays,sets, and linked lists are predefined in class definitions, and are created by usingthe new: keyword method for the respective class.

Example goes here:

Page 11: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

User-defined types are handled by the inheritance of methods in classdefinitions.

Example:

Page 12: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Conditionals

WhileA loop which functions as a while loop from C++ would be implemented in Smalltalkin the following way:

i:=0.[(i<5)]whileTrue:[Transcript show: ' Number: '. Transcript show: (i). i:=i+1.].

“This code generates the following output:”

Number: 0 Number: 1 Number: 2 Number: 3 Number: 4

Page 13: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

If / Else

a:=5.b:=2.((a+b)<10) "this is the evaluated expression"ifTrue: [Transcript show: 'less than ten'; cr.]ifFalse: [Transcript show: 'greater than or equal to ten'; cr.].

“The above code produces the following: “

less than ten

Page 14: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

If (continued)

You can use a single ifTrue or ifElse with a precedingexpression to be evaluated, and a following statement block,or you can use them together, as in the previous example,as long as you only put a period after the last block.

Page 15: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Some basic syntax

There is no need to import libraries.

The end of line command is a period.

“This is a comment in Smalltalk. Use double quotes.”

Variable declarations are done like this: |x y z| “no type specifications for variables.”

Page 16: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Some more basic syntax

Smalltalk is interpreted, so there is no compilation procedure.

To run your code, select all of the text, left-click, and choose‘do it’ from the menu.

Assignment is done either as x_1 or x:=1.

Equality is checked with a single x=y (rather than == of C++).

Brackets are the block delineators [code goes here]

^value is how you signify a return value.

Page 17: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Data objects:

Classes define types in Smalltalk. Variable naming conventions in Smalltalksuggest that strings of letters of multiple words be used. The first letter should belower-case (upper-case in a global variable), and successive words should beginwith a capital letter: myVariable, anInteger, etc… Strings are enclosed by singlequotes.

Smalltalk is an interpreted language, so execution is dynamic; variable types arenot specified.

Page 18: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

The For Loop

Similar to Fortran, the Smalltalk For loop is done with ato:by:do: statement.

1 to: 100 by: 20 do: [:x | Transcript show: ‘hello world ‘; cr.]

“The above code produces the following: “

hello worldhello worldhello worldhello worldhello world

Page 20: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Comments and Literals

Comments “here is one comment that can span many many lines” Numbers instances of class Number (Integers, Floats, Fractions) Characters (Instances of class Character) character preceded by a dollar sign $x $3 $< $$ Strings (Instances of class String) ‘sequence of “with comment inside” characters’

Page 21: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Pseudo-variables are reserved identifiers that are similar to keywords in other languages.

nil true false are constants.

self - the current object, that is, the receiver of the current message; like (this in C++)

super - refers to the same object as self. However, the search for a suitable method starts in the superclass of this class.

Pseudo - Variables

Page 22: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Message Expressions

The association of a method with an object is called a message. Message expressions are sequences of messages sent to objects. Expressions consist of selectors followed by their arguments if

any. Messages names should indicate the type of action desired. There are three types of messages:

Unary messages: (new abs factorial sqrt asString)Binary messages: (+n -n *n /n \\ -”remainder”)Keyword messages: (6 between: 3 and: 9 - “true”) more on messagesAvailable : http://mucow.com/SqueakClassesRef.html

Page 23: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Unary messages

Messages without arguments.

Unary message to a class: ’new’ returns a newly created instance of a class MyFriend

Messages that are most tightly parsed left to right.

“same as (((9 sqrt)rounded)asString)

9 sqrt rounded asString

|friend| friend := MyFriend new

Page 24: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Binary Messages

Used for arithmetic operations

Binary messages are always parsed left to right, regardless

of the precedence of numeric operations.

Use of parentheses is a good idea!!!!!!

3+4

7 3+4*5 35 3+4factorial

27

Page 25: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Keyword messages

Use keywords to name arguments. stores value in array at index

same, but stores at index factorialMessages can be nested.Parsing order is left to right, precedence is:

unarybinarykeyword, use parentheses if necessary.Messages sent to the same receiver may be “cascaded” just use semicolons;

array at: 3 put: 12

array at: 3factorial put 12

receiver show; +23; at:23 put: 4

Page 26: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Example in Squeak Windows

Page 27: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Block Expressions

Block without arguments [expressionSequence] Block with arguments and local variables.

[ (:identifier)+||identifier+|expressionSequence]

block that an argument global message new is an object variable line

name

1 to: 5 do: [:I | (Transcript show: ‘Hello friend’) cr]

Page 28: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

It’s true us long us it’s not falseifTrue: alternativeBlock ifFalse: alternativeBlock.They can be used separately or together to form if/else block.If used together, put a period after the second block, but not the first.

Page 29: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Inheritance

Object is the root of Smalltalk’s inheritance hierarchy, providing some basic functionality to all objects.

Smalltalk only supports single inheritance, but methods can be passed through multiple levels of parent classes.

If any method is not defined for an object, the parent of that object is called with the same method.

Page 30: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Inheritance

Object Magnitude Number Float Integer Fraction Character Boolean False True

Page 31: Smalltalk (and Squeak) Aida Dungan and Rick Shreve

Building an application