Where to 'declare' symbols

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Where to 'declare' symbols

John Almberg
I want to eliminate a 'magic number' used by methods in a single  
class. I'd like to do that by creating a symbol that would be  
available to all the methods in the class. Where and how should I  
create such a symbol? I can't seem to find an example anywhere.

Thanks: John

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Where to 'declare' symbols

John Almberg
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?

-- John


On Aug 13, 2007, at 9:43 PM, John Almberg wrote:

> I want to eliminate a 'magic number' used by methods in a single  
> class. I'd like to do that by creating a symbol that would be  
> available to all the methods in the class. Where and how should I  
> create such a symbol? I can't seem to find an example anywhere.
>
> Thanks: John
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Where to 'declare' symbols

Tapple Gao
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
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Where to 'declare' symbols

John Almberg
Hi Matthew,

Oh, duh! A method that simply returns a value *is* a simple solution.  
Thanks.

I just tried the class variable approach, too, (just to see how it  
works). It took me a while to figure out what you meant by  
initializing the Class but finally realized the only way to access  
the class variable was to do something like

MyClass initialize.
MyClass MySpecialConstant.

I guess inside a class method, you could access it like:

testValue > MySpecialConstant

Okay, I think I get it.

Thanks!

-- John


> "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
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Websites for On-line Collectible Dealers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Identry, LLC
John Almberg
(631) 546-5079
[hidden email]
www.identry.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners