smalltalk Smalltalk Syntax Classes and methods


Classes and methods are usually defined in the Smalltalk IDE.


A class definition looks something like this in the browser:

XMLTokenizer subclass: #XMLParser
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'XML-Parser'

This is actually the message the browser will send for you to create a new class in the system. (In this case it's #subclass:instanceVariableNames:classVariableNames:poolDictionaries:category:, but there are others that also make new classes).

The first line shows which class you are subclassing (in this case it's XMLTokenizer) and the name the new subclass will have (#XMLParser).

The next three lines are used to define the variables the class and it's instances will have.


Methods look like this in the browser:

aKeywordMethodWith: firstArgument and: secondArgument
    "Do something with an argument and return the result."

    ^firstArgument doSomethingWith: secondArgument

The ^(caret) is the return operator.

** anInteger
    "Raise me to anInteger"
    | temp1 temp2 |

    temp1 := 1.
    temp2 := 1.
    1 to: anInteger do: [ :i | temp1 := temp1 * self + temp2 - i ].

this is not the right way to do exponentiation, but it shows a binary message definition (they're defined like any other message) and some method temporary variables (or method temporaries, temp1 and temp2) plus a block argument (i).