I imagine that you know some programming languages: Java or JavaScript or C++ or C# (.NET) etc. It's possible that you love one and hate all the others. This is natural. But try to open your mind. Today, a good professional needs to work with many different languages.
We will talk now about the main concepts of the main language used at Cobalt: Smalltalk.
A Class is a group of methods (functions, subroutines, messages) that can be performed using an object (an instance, an object-reference, a prefix, a pointer) of this Class.
To create an object of the Class X, by example, we need to write the statement:
x := X new.
The symbol ":=" is the operator for assignment in Smalltalk.
All statement finish by a "point".
To execute the method theMet of the Class X, by example, we need to write the statement:
x theMet1.
A method can have a parameter and the sintax will be something like:
x theMet2:par1.
A parameter can be another object of another Class.
TIP: Example for many parameters: if you like to create a method that receives 3 parameters and returns their sum:
sumOfThree:a with:b plus:c
^ (a+b+c).
It's usual, in Smalltalk, to say that: " the object x received the message theMet1" meaning that this method was executed.
A Class has a " constructor method" whose name will be: initialize. When a Class is instancialized, its " constructor" will be executed.
In the definition of the statements of a method we can put a call for another method of the same or of another Class (that needs to be instancialized first). To use a method of the same Class we use the prefix: self like the object.
Some methods contains variables that need to be defined at the begining of the method inside bars;
Example:
| var1 var2 var3 |
These variables are local to this method.
It's not usual the definition of "properties" in Smaltalk. Methods are used to simulate them. We can have, by example, a method color:c, meaning that, if we use:
x color: cthe object x has the color c.
Smaltalk accepts inheritance. This means that we can create a subClass Y for a Class X. This subClass will have all the methods of its superClass.
We will learn how to create subClasses in future lessons. Cobalt has many Classes that we will "subclass".
If Y is a subClass of the Class X having the method theMet1 we can do, by example:
y := Y new.
y theMet1.
and the method theMet1, defined in the Class X, will be executed.
We can modify a method of a superClass writing a new method in the subClass having the same name of that of the superClass. We say that the new method "overrides" the other.
If we don't override the "initialize" method of the superClass it will be executed when we instancialize the subClass.
A group of Smalltalk Classes (including methods etc.) is called: an image. Different "images" of Smaltalk can be distributed by different companies, user groups, entities etc. By example: the image distributed by the group "Squeakland" is different of that distributed by the group "Croquet". If YOU create new subClasses and/or methods, you can save your work (we call this a "big save" - we will talk about this soon) creating YOUR "image".
Smalltalk breaks out of the "compile-link-run" cycle. Changes in a method in some Class, will change all the use of this method at all applications running under this "image". Be carefull.
It's usual that Class names begin with a capital letter such as "Student". Method names begin with a lower case letter and can have any combination of letters and numbers without embedded blank spaces. When a method name contains more than one word, the extra words start with a capital letter.
A method name would be, by example: "aMessage" or "myAddress."
It's usual that the name of a method means a message. A message such as "give me your name" could be "giveMeYourName:n". This however, is too difficult to work with. It would be easier to shorten the designation of the method to just: "name:n".
If a parameter is a string it's placed inside 'apostrophes'. By example:
st Student new.
st name:'Peter Brow'.
Pay attention because here "name" is not a property, and it would be wrong to write:
st name := 'Peter Brow'.
In Smalltalk, the last line of a group of statements doesn't need to finish with "point", but we recomend not use this facility.
Smalltalk has a series of execution-time rules for evaluating a Smalltalk statement. There are three important rules for evaluating code:
To have a declared "return" of a method, you need to place a caret (^) symbol in front of the variable whose value will be returned . It's important that the line of code having the return to be the last because when a ^ is encountered, the method ends execution and returns the value of the variable following the ^ return symbol.
TIP: Sometimes you can use the caret in the last statement. Something like:
^x := y + 29. "return the value of x"
In Smalltalk, comments are enclosed in double quotations. We sugest that you use many comments inside your programs when doing it. This will be the best documentation. If you let to do the documentation after the tests, you never you do it...
If you use a variable like a parameter in the definition of the name of a method you don't need to declare it inside the method:
aMethod: aParameter
^aParameter * 45
Smalltalk has resources to programming comparisons. It's a little different from Java. C# or C. Look the IF THEN equivalent:
| x y |
x := 1.
y := 2.
(x > y)
ifTrue: [
"do something".
]
ifFalse: [
"do something".
].
You can write many lines of code between the square brackets.
For comparisons we use:
> "greater than"
< "less than"
= "equal to in value"
~= "not equal in value"
>= "greater than or equal to"
<= "less than or equal to"
The "==" comparison, checks to see if the two objects are the same object, rather than equal in value.
And the signals for AND and OR are:
(a > 0) & (b < 0) "Returns true if a is positive
and b is negative,Otherwise false."
(a > 0) | (b < 0) "Returns true if either a is
positive or b is negative."
You can use the "return symbol" inside a comparison:
(m < n)
ifTrue: [^m]
ifFalse:[^n]
This code returns the value of n if n is greater than m , otherwise it returns the value of m .
We said that: to declare an string you need to put it inside single quotes. You can declare a character using the symbol "$" to preced it:
someChar := $K.
A number can not begin with decimal point or end with decimal point:
someValue := .57 "ERROR!!!!!!!"
The arithmetic operation signals in Smalltalk are:
+ "addition" - "subtraction" * "multiplication" / "division" // "division with truncation" \\ "remainder from division"Some examples of arithmetic operations are follows:
5 + 4. "returns 9" 9 // 4. "returns 2" 9 \\ 4. "returns 1" | x y | x := 5. y := 7. x * y "returns 35"
In Smalltalk, nil means "nothing". All variables initially are point to nil .
The usual LOOP FOR is declared in Smalltalk like this:
1 to: last do: [ :i | i+1. "do something" ]
This can be a little confuse for a Java, C# or C programer. Lets go to see an example, comparing:
The LOOP FOR that you know would be:
for(i=1; i<=4; i++){
x = i*5;
}
In Smalltalk you will have:
1 to:4 do: [
: i | i+1.
x := i*5.
]
And lets go to see an example of the LOOP WHILE in Smalltalk:
x := 5.
y := 0.
[y <= x] whileTrue: [
"do something".
y := y + 1
].
We will have the 6 roundtrips.
I accept that, for old programmers - like me - can be not very easy to use, in the beginning...
PREVIOUS LESSON NEXT LESSON TABLE OF CONTENTS HOMEPAGE