4: Syntax in a Nutshell
- Page ID
- 39590
Pharo adopts a syntax very close to that of its ancestor, Smalltalk. The syntax is designed so that program text can be read aloud as though it were a kind of pidgin English. The following method of the class Week
shows an example of the syntax. It checks whether DayNames
already contains the argument, i.e. if this argument represents a correct day name. If this is the case, it will assign it to the variable StartDay
.
startDay: aSymbol (DayNames includes: aSymbol) ifTrue: [ StartDay := aSymbol ] ifFalse: [ self error: aSymbol, ' is not a recognised day name' ]
Pharo’s syntax is minimal. Essentially there is syntax only for sending messages (i.e. expressions). Expressions are built up from a very small number of primitive elements (message sends, assignments, closures, returns...). There are only 6 keywords, and there is no syntax for control structures or declaring new classes. Instead, nearly everything is achieved by sending messages to objects. For instance, instead of an if-then-else control structure, conditionals are expressed as messages (such as ifTrue:
) sent to Boolean objects. New (sub-)classes are created by sending a message to their superclass.