Difference between revisions of "Smalltalk"

From GNUstepWiki
Jump to navigation Jump to search
 
Line 66: Line 66:
 
  ( 1 = 1 ) ifTrue: [ Transcript showLine: 'Yes']
 
  ( 1 = 1 ) ifTrue: [ Transcript showLine: 'Yes']
 
  ( 2 < 1 ) ifFalse: [ Transcript showLine: 'No']
 
  ( 2 < 1 ) ifFalse: [ Transcript showLine: 'No']
  ( 1 >= 4 ) ifTrue: [ Transcript showLine: 'Yes'] ifFalse: [[ Transcript showLine: 'No']
+
  ( 1 >= 4 ) ifTrue: [ Transcript showLine: 'Yes'] ifFalse: [ Transcript showLine: 'No']
  
 
=== Cycles ===
 
=== Cycles ===

Revision as of 11:59, 18 February 2005

Smalltalk is pure object oriented language with environment. In the context of the GNUstep, the Smalltalk is a Scripting language bundle for the StepTalk scripting framework.

Note that the language bundle offers Smalltalk language only, that sits on top of the Objective C object environment. Therefore, some classes and mechanisms known from Smalltalk are not (yet) available.

The Language

The language has very straightforward and easy to learn syntax. It is said, that the syntax was inspired somehow by the english grammar.

Basics

The language is based on sending messages to objects, like:

anObject message

Here are few examples that you should try, for example by using the StepTalk shell. For example to see current date, just send message date to the object NSDate:

NSDate date

To assign a result to a variable:

date := NSDate date

or

a := 10.
b := 20.
result := a + b.

Literals and basic operations

In a script you should have literal objects: strings, arrays, numbers and selectors.

Strings 'This is a string' 'This is another \'String\' with apostrophes'
Array #(1 2 3) #('one' 'two' 'three')
Numbers 1 2 0.1 -1.1e10

To join two strings use , (comma), like:

'This is ', 'a string'

To get an object from an array (indexed from 0):

#('one' 'two' 'three') @ 1

To do computations with numbers:

1 + 1

Important note: Smalltalk does not take into account operator priority, so you have to enclose an expression into parentheses. Try:

1 + 2 * 3
1 + (2 * 3)

You should use operators: + - * /

Blocks

Instead of explaining what blocks enclosed by [ ... ] are, try following examples, line by line, and you will get the idea:

( 1 = 1 ) ifTrue: [ Transcript showLine: 'Yes']
( 2 < 1 ) ifFalse: [ Transcript showLine: 'No']
( 1 >= 4 ) ifTrue: [ Transcript showLine: 'Yes'] ifFalse: [ Transcript showLine: 'No']

Cycles

Cycles are created using blocks:

1 to: 10 do: [[:i | Transcript showLine: (i description) ]
0 to: 100 step: 10 do: [[:i | Transcript showLine: (i description) ]

Other possibilities are:

conditionBlock whileTrue: toDoBlock.
conditionBlock whileFalse: toDoBlock.

like:

a := 0 . [[ a < 10 ] whileTrue: [[ a := a + 1 . Transcript showLine: (a description) ]

Array iteration

array := #('one' 'two' 'three').
array do: [[:item | Transcript showLine: item ]
array select: [[:item | item hasPrefix:'t' ]
do: Evaluate block for each object in array and return last evaluated expression
select: Create new array which will contain those objects from receiver, for which value of block was true
reject: Create new array which will contain those objects from receiver, for which value of block was false
collect: Create new array from block return values.
detect: Return first object for which block evaluation result is true. Othervise return nil.

More information and examples

More examples are included in the StepTalk source distribution in Examples directory, please refer to that place.

For more information, please refer to the Documentation directory. For language reference see Smalltalk.txt file.