Usage

Why is there no goto statement?

Tagged:

Anticipated return can be used to jump out of a method, and that's enough to emulate structured gotos like C's break statements.

Otherwise, you can use exceptions to provide a "structured goto" that even works across function calls. Exceptions are particularly powerful in Smalltalk, as they allow resuming the execution at the point the exception was thrown, as well as retrying the execution of the piece of code that triggered an exception.

^[ self assert: (Date daysInMonth: #feb forYear: Date today year) = 29 ]
    on: Error
    do: [ :ex |

Why is there no switch statement?

Tagged:

Well, various syntaxes have been proposed along the years, but no one was really satisfying.

There are many alternatives:

  • You can use #ifTrue:ifFalse: repeatedly.
  • You can create a dictionary or, in more complex cases, a dictionary of blocks
  • You can use polymorphism

Huh, so why can't this be changed?

Tagged:

There are already millions of lines of Smalltalk code around the world, so any change in the language that invalidates more than a very small fraction of existing programs has to be frowned upon.

However, in the future, GNU Smalltalk may warn about such constructs. If you feel this is very important for you, bug people enough so that they listen and implement this feature.

Why am I getting strange results with simple arithmetic operations?

Tagged:

Or, why is 3 + 5 * 4 = 32?

For some people, this is the least cool feature of Smalltalk. Operators don't have any precedence in Smalltalk and expressions are evaluated left-to-right:

3 + 5 * 4 = (3 + 5) * 4 = 8 * 4 = 32
3 + 4 * 5 = (3 + 4) * 5 = 7 * 5 = 35

In practice, this rarely matters, but it is still something to be aware of.

What platforms does GNU Smalltalk run on?

Tagged:

GNU Smalltalk is developed and routinely tested under Linux and Darwin (the kernel of Mac OS X), and should run under most Unix systems (as well as under Windows, using Cygwin). A native port to Windows is in progress.

Syndicate content

User login