logo       

Re: Where to 'declare' symbols: msg#00120

lang.smalltalk.squeak.beginners

Subject: Re: Where to 'declare' symbols

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


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise