[Newbies] constants

Norbert Hartl norbert at hartl.name
Sun Sep 28 13:33:44 UTC 2008


On Sun, 2008-09-28 at 07:54 -0500, Mark Volkmann wrote:
> How are constants represented in Smalltalk? Do you create a method  
> whose name is the name of the constant and return the value?
> 
You have several ways to define something you could call
"constant". If we see a constant as a global accessible
thing that doesn't change and has something like a name
you have multiple options:

- #myConstant Symbol. That is globally available and is always
  the same object.

- a class. A class is an object with a name that is globally
  accessible and gives the same object everytime

- cached instances. You define class methods that create
  instances and cache the instances. This way you get
  the same object everytime you invoke the class method

I think there even more possibilities. What you should
use depends on what you are trying to achieve. A symbol
is just the name. A class serves this purpose quite well
for a lot scenarios. But you don't want to create classes
only because you need some constant value. 

Norbert



More information about the Beginners mailing list