Howto initialize class variables

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

Howto initialize class variables

Alex Schenkman
Hi List!

I'm playing with class variables. How should I initialize them?
I've tried with the class method initialize but it does not do the job.

Pirulo class>>initialize
    super initialize.
    myClassVar := Dictionary new at: 'one' put: 1.


Thanks in advance!


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

Re: Howto initialize class variables

Bert Freudenberg
On 20.03.2010, at 09:46, Alex Schenkman wrote:
> Hi List!
>
> I'm playing with class variables. How should I initialize them?
> I've tried with the class method initialize but it does not do the job.
>
> Pirulo class>>initialize
>     super initialize.
>     myClassVar := Dictionary new at: 'one' put: 1.

* never call "super initialize" on the class side
* class variables should be capitalized
* class initializers need to be executed manually
* they are only executed automatically when loading the class (e.g. file-in or via Monticello)

So this would be right:

Pirulo class>>initialize
    "self initialize"
    MyClassVar := Dictionary new at: 'one' put: 1.

The "self initialize" comment is conventionally put there so you can easily double-click after the opening quotes to select the whole expression, and do-it. You need to do it any time you change the initialize method. This works because when evaluating code in a Browser, "self" refers to the currently selected class.

- Bert -


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

Re: Howto initialize class variables

Alex Schenkman
Bert your are super!

* never call "super initialize" on the class side
 Why? I thought it was "polite" to do it, =)

On Sat, Mar 20, 2010 at 10:10, Bert Freudenberg <[hidden email]> wrote:
On 20.03.2010, at 09:46, Alex Schenkman wrote:
> Hi List!
>
> I'm playing with class variables. How should I initialize them?
> I've tried with the class method initialize but it does not do the job.
>
> Pirulo class>>initialize
>     super initialize.
>     myClassVar := Dictionary new at: 'one' put: 1.

* never call "super initialize" on the class side
* class variables should be capitalized
* class initializers need to be executed manually
* they are only executed automatically when loading the class (e.g. file-in or via Monticello)

So this would be right:

Pirulo class>>initialize
   "self initialize"
   MyClassVar := Dictionary new at: 'one' put: 1.

The "self initialize" comment is conventionally put there so you can easily double-click after the opening quotes to select the whole expression, and do-it. You need to do it any time you change the initialize method. This works because when evaluating code in a Browser, "self" refers to the currently selected class.

- Bert -


_______________________________________________
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: Howto initialize class variables

Randal L. Schwartz
>>>>> "Alex" == Alex Schenkman <[hidden email]> writes:

Alex> Bert your are super!
Alex> * never call "super initialize" on the class side

Alex>  Why? I thought it was "polite" to do it, =)

It breaks things by double initializing them.

The code loaders run initialize automatically.  So when the parent
class was loaded, it was already initialize.  You've just asked it
to do that again.

On the *instance* side, you *must* call super initialize, because
you don't get another chance to do that.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Howto initialize class variables

David T. Lewis
In reply to this post by Bert Freudenberg
On Sat, Mar 20, 2010 at 10:10:10AM +0100, Bert Freudenberg wrote:

> On 20.03.2010, at 09:46, Alex Schenkman wrote:
> > Hi List!
> >
> > I'm playing with class variables. How should I initialize them?
> > I've tried with the class method initialize but it does not do the job.
> >
> > Pirulo class>>initialize
> >     super initialize.
> >     myClassVar := Dictionary new at: 'one' put: 1.
>
> * never call "super initialize" on the class side
> * class variables should be capitalized
> * class initializers need to be executed manually
> * they are only executed automatically when loading the class (e.g. file-in or via Monticello)
>
> So this would be right:
>
> Pirulo class>>initialize
>     "self initialize"
>     MyClassVar := Dictionary new at: 'one' put: 1.


One other note - In this example you probably intended to save the
actual dictionary in MyClassVar, in which case you would do this:

    MyClassVar := Dictionary new at: 'one' put: 1; yourself.

This is because the #at:put: message answers the thing you added,
not the dictionary, so sending the cascaded #yourself message
will answer the dictionary itself and save it in MyClassVar.

Dave

> The "self initialize" comment is conventionally put there so you can easily double-click after the opening quotes to select the whole expression, and do-it. You need to do it any time you change the initialize method. This works because when evaluating code in a Browser, "self" refers to the currently selected class.
>
> - Bert -
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners