[Newbies] Re: Alternative for static const...

Mispunt mispunt at users.sourceforge.net
Tue Feb 27 12:33:30 UTC 2007


After some better testing, It doesn't work :S It seems that the
initialize of the constants class isn't running... (the normal
classVariableNames are working...) I have to use the poolDictionary
because there are some codes that return in a lot of classes..

Mispunt

On 2/27/07, Mispunt <mispunt at users.sourceforge.net> wrote:
> Thanks Bert, it works. When I am using the poolDictionary it isn't
> possible to use the constants in the Workspace right? or do I have to
> write methods for them when I want to use them? (well I don't want to,
> because it is just for testing purpose).
>
> Mispunt.
>
> On 2/27/07, Bert Freudenberg <bert at freudenbergs.de> wrote:
> > On Feb 27, 2007, at 10:08 , Mispunt wrote:
> >
> > > Hi all,
> > >
> > > For programming my Lego Mindstorms NXT I have to use codes like 16r1F.
> > > But I actually want to be able to use "readable" codes. In a language
> > > like Java I will do that with a static const, but as far as I know I
> > > have to do it with symbols in Smalltalk.
> >
> > No, there is a better way.
> >
> > If you are inside one class (or its subclasses), you would use "class
> > variables", one per constant. You add them in the class template:
> >
> > Object subclass: #Bla
> >         instanceVariableNames: ''
> >         classVariableNames: 'Const1 Const2'
> >         poolDictionaries: ''
> >         category: 'Bert-Bla'
> >
> > and the initialization code is in a class-side #initialize method:
> >
> > initialize
> >         "self initialize"
> >         Const1 := 16r1F.
> >         Const2 := 12345.
> >
> > You need to execute the "self initialize" in the browser to do the
> > initialization. It will be executed automatically when loaded into
> > another image.
> >
> > Class variables are capitalized because they are sort-of "global",
> > they can be used in the defining class, its metaclass, and all their
> > subclasses. If you need to give access to these variables across the
> > class hierarchies, then you would do the same, but as a subclass of
> > SharedPool:
> >
> > SharedPool subclass: #BlaConstants
> >         instanceVariableNames: ''
> >         classVariableNames: 'Const1 Const2'
> >         poolDictionaries: ''
> >         category: 'Bert-Bla'.
> >
> > To use this "pool" of variables in another class, list it as a "pool
> > dictionary":
> >
> > Object subclass: #Bla
> >         instanceVariableNames: ''
> >         classVariableNames: ''
> >         poolDictionaries: 'BlaConstants'
> >         category: 'Bert-Bla'
> >
> > This makes all class variables of BlaConstants available to Bla as if
> > they were class variables.
> >
> > There are quite a few other ways to allow "global" variables in
> > Smalltalk, but this one is the "clean" way to do it which works
> > nicely with tools like Monticello.
> >
> > - Bert -
> >
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at lists.squeakfoundation.org
> > http://lists.squeakfoundation.org/mailman/listinfo/beginners
> >
>


More information about the Beginners mailing list