"Bitwise Magazine Smalltalk Tutorial - Part One" " * Smalltalk Sample Code provided by BITWISE MAGAZINE * * http://www.bitwisemag.com * * This code and information is provided 'as is without warranty of * any kind, either expressed or implied, including but not limited * to the implied warranties of merchantability and/or fitness for a * particular purpose. * * You may use and modify this code for your personal use only. * You may also redistribute this code providing that * a) No fee is charged and b) This copyright notice * is retained in the source code. * * You may not use this code for commercial purposes * without the express permission of the copyright holder * * Copyright (c) 2005 Huw Collingbourne " "Note: This sample code has been written using Squeak Smalltalk - from http://www.squeak.org. In most cases, it should work with other Smalltalk implementations too. You should load or copy this file into a blank workspace (that is, do not use it inside one of the Smalltalk browser panes!) You must Evaluate/Do or Print/Display each line. The keystrokes or menus needed to do this will vary between Smalltalk implementations. For example, in Squeak Evaluate/Do is Alt-D; in Dolphin Smalltalk, it is Ctrl+E. In Squeak, Print/Display is Alt-P; in Dolphin Smalltalk, it is Ctrl-D. Evaluate lines of code in this document in the order in which they appear. If you don't do this, objects required by code later in the document may not have been created, so that some examples won't run." " --- CLASSES, OBJECTS AND MESSAGES --- " "In Smalltalk, everything is an Object - for example, the number 100, and the word 'Hello' are both Objects. And every object belongs to a class. You can ask an Object to identify itself. Print or Display each of these expressions: " 100 class. 1000000000000 class. 'Hello' class. #(a b c ) class. Smalltalk class. "In the examples above, 'class' is a message. In Smalltalk, you get things done by sending messages to Objects. Some messages, such as 'class', take no arguments. Others take other objects are arguments. For example, the String message 'at:' takes an integer argument, whereas the number message '*' takes another number as an argument. Display these:" 2 * 10 3 * 3 'Smalltalk is wonderful!' at: 1 'Smalltalk is wonderful!' at: 2 "Some messages have more than one part. For example, copyFrom:to: takes two arguments, one after the first part, copyFrom:, another after the second part, to:. Make sure to place a colon at the end of these messages. Try out the following:" 'Smalltalk is wonderful!' copyFrom: 1 to: 9 'Smalltalk is wonderful!' copyFrom: 1 to: 3 * 3 " --- FROM OOP TO OOPS! - THE SMALLTALK DEBUGGER --- " "Try out these deliberate bugs. When you see the error dialog, select Debug. Take a look at the information in the debugger then close it to return to this document." 'Smalltalk is wonderful!' copyFrom: 1 to: 90 123456789 copyFrom: 1 to: 9 '20' squared. " --- VARIABLES AND SCOPE --- " "There are various sorts of variable, each of which has a different scope. A workspace variable lives inside the environs of a workspace such as this window. Evaluate the following:" i := 10. i. i class. "In the above code, Smalltalk created a SmallInteger object called i. But it can re-use this variable name and re-create it as an object of some other type. Display the following lines to see this in practice:" i := 'Hello from the Bitwise Smalltalk Tutorial Workspace!' i. i class. "Now open a new workspace by selecting the File menu, then New. Enter i into your second workspace and display it. It won't recognise the i variable. Now switch back to this workspace. Evaluate the following:" Hello := 'Hello from the world of Smalltalk!' "When promted, define Hello as a global variable. Now Display this:" Hello. "Switch to the 2nd Workspace, enter Hello and Display it. This time, it knows all about the Hello variable. That's because a variable name that starts with a capital letter, such as Hello, is global. It is known to the entire Smalltalk system. A variable that starts with a small letter, such as i is only known in its own Workspace. Ask Smalltalk to remove Global Variables once you've finished with them. Send the removeKey message to Smalltalk, passing to it the name of the Global variable, preceded by a hash (#) symbol. Evaluate the following:" Smalltalk removeKey: #Hello. "Check that the Hello variable has been removed. Try to Display the following:" Hello. "Assigning values. Display each of the following, one line at a time." x := 'Hello'. y := 'world'. z := x , ' ' , y. z. "Note the effect of the comma in the above. It joins together or 'concatenates' the strings" "--- ARRAYS ---" "Examples of some arrays. Display each of them." strlist := #('one' 'two' 'three'). intlist := #(1 2 3).#(1 2 3). mixedlist := #( 'one' 2 (3 4 5) $6 ). "What type of object is at subscript 3 of this array....?" (mixedlist at: 3) class. "Let's put a different object at subscript 2." mixedlist at: 2 put: strlist. mixedlist. "Recall how we used a comma to concatenate strings earlier. Now try it out with arrays. Display the following:" #(1 2 3 ), #(4 5 6). strAndIntList := strlist , intlist. strAndIntList. "--- CASCADING MESSAGES ---" "Evaluate these two lines. Make sure the System Transcript window is visible. In Squeak, you can drag a Transcript out of the Tools palette. In Dolphin Smalltalk, select System Transcript from the Tools menu." ob := strlist at: 2. Transcript cr. Transcript show: ob. "In the above example, we have written two statements, each ending with a full stop, each passing a single message (cr and show:) to Transcript. We can actually send multiple or 'cascading' messages to the same receiver object (here Transcript) by separating each message with a semi-colon. Evaluate the following:" Transcript cr; show: ob. "--- USE THE printString MESSAGE TO DISPLAY DATA ---" "Now evaluate each of these lines one by one" Transcript cr; show: 10. "In some Smalltalks (e.g. Dolphin) you'll have a problem showing an integer." "Other Smlltalks (such as Squeak) will let you show an integer" Transcript cr; show: 10 printString. "You can always print an integer as a String though (should work with all Smalltalks)" Transcript cr; show: 10 class printString. "you can print a class as a String too" 10 timesRepeat: [Transcript show: 'Hello'; cr]. "Now mark off and evaluate this entire code block (be sure to include the first line: | a |. Note: You must have evaluated mixedlist earlier in this document in order for this to work." | a | a := 1. mixedlist size timesRepeat: [ ob := mixedlist at: a. Transcript cr; show: '[', a printString , '] ', ob printString, ' is an object of the Class: ', ob class printString. a := a + 1. ]. "In the above, a is a temporary variable. It is discarded after the code has been run. Verify it by attempting to display a:" a. "Temporary variables are indicated by enclosing them between vertical bars. Mark off this code block and Display it. Note that the ^ symbol at the end tells Smalltalk to Return the value following it." | temp1 temp2 temp3 | temp1 := 10. temp2 := '...Hello '. temp3 := 'world'. ^temp1 printString, temp2, temp3. "Now evaluate this:" temp1.