[Newbies] Where to 'declare' symbols

Matthew Fulmer tapplek at gmail.com
Tue Aug 14 02:10:39 UTC 2007


On Mon, Aug 13, 2007 at 09:53:12PM -0400, John Almberg wrote:
> Sorry... probably 'symbol' isn't the right word, since as far as I  
> know, something like #aSymbol doesn't have any value other than it's  
> own identity.
> 
> What I really want is a named constant that has a value. Like  
> iAmAConstant = 5. Is there such a thing in Smalltalk?
>

Two ways; both are common.

"First, and simplest, implement a message that returns the
constant:"

MyClass >> mySpecialNumber
    ^ 5

"Second, use a class pool variable:"

Object subclass: #MyClass
    instanceVariableNames: ''
    classVariableNames: 'MySpecialConstant'
    poolDictionaries: ''
    category: 'MyCategory'

"Set its value in the CLASS-SIDE initialize method:"

MyClass class >> initialize
    MySpecialConstant := 5

"Don't forget to call MyClass initialize. Monticello will
automatically do this ONCE"

MyClass initialize

-- 
Matthew Fulmer -- http://mtfulmer.wordpress.com/
Help improve Squeak Documentation: http://wiki.squeak.org/squeak/808


More information about the Beginners mailing list