[Newbies] constants

Mark Volkmann mark at ociweb.com
Sun Sep 28 13:56:43 UTC 2008


On Sep 28, 2008, at 8:33 AM, Norbert Hartl wrote:

> 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.


I think my main issue is scoping. I want to define a constant that is  
associated with a class to avoid name conflicts. Maybe that's not the  
Smalltalk way. Similarly I'm concerned about defining a class, then  
later installing a library that contains a class with the same name as  
one of mine. Does that issue come up very often? I'm concerned about  
the lack of support for namespaces (packages in Java).

---
Mark Volkmann






More information about the Beginners mailing list