Accessors

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

Accessors

Jerome Peace
Hi Blake,

In squeak we usually just leave the garbage for the
maid to pick up.

However:
myCollection := nil.
or
myCollection := #() .
will clear the collection.

myCollection withAll: nil .
leaves the collection its original size and resets
contents.

On accessors versus raw IVars. its a matter of
programmer choice. If the data is hidden from other
classes it can be handled by using just the iVars
within the class w/o an accessor.

Your probably about ready for Kent Beck's book
"Smalltalk: best programming pattern practices." Which
will answer a lot of the questions you are about to
ask.

If you try to do the "simplest thing that might
possibly work." Then you would probably start with the
ivars and switch over to using accessors when there
becomes a need.


***
>  [Newbies] Accessors
>  
>  Blake blake at kingdomrpg.com
>  Mon Mar 12 01:36:12 UTC 2007 wrote:
>  
>  I've been using accessors for every instance
variable,
>  and even using them  
>  internally.
>  
>  I have to admit, because of the need to preface
every
>  method call with  
>  "self", it makes the code look less clean.
>  
>  I've also ended up with what seems to be an odd
>  construct:
>  
>  clear
>   self myCollection removeAll: self myCollection
>
Go reread the comment for #removeAll I believe you
will find it does not do what you wish.

One of the necessary ways to learn squeak is to try
out your ideas in a work space.

Try:

myCollection := OrderedCollection new
        add: 4
        ; add: 3
        ; add: 2
        ; add: 1
        ; yourself.
myCollection removeAll: myCollection  .

and see what you get. See if you can figure out why.

Then RTFM. Read the fine manual of Kent Beck.
And RTFC. Read the fine comments (or code) in squeak
itself.


 
>  Part of this is the self thing:
>  
>   myCollection removeAll: myCollection
>  
>  and the other part is...I guess, not being able to
find
>  a "clear"
>  
>   Thanks for all the feedback guys. Very helpful.
>  
>   I've been too lazy with my Smalltalk (thinking) in
the
>  past and this has  
>  helped a lot.
>  
***


Yours in service, --Jerome Peace


 
____________________________________________________________________________________
The fish are biting.
Get more visitors on your site using Yahoo! Search Marketing.
http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Accessors

Blake-5
On Sun, 11 Mar 2007 18:46:00 -0800, Jerome Peace  
<[hidden email]> wrote:

> However:
> myCollection := nil.
> or
> myCollection := #() .
> will clear the collection.

Since I want it empty, not gone, I'm doing this:

myCollection := OrderedCollection new.

> On accessors versus raw IVars. its a matter of
> programmer choice. If the data is hidden from other
> classes it can be handled by using just the iVars
> within the class w/o an accessor.

Yeah, I guess. I've always tried to keep the object as close to a client  
of itself as I can but that's a decent rule of thumb.

> Your probably about ready for Kent Beck's book
> "Smalltalk: best programming pattern practices." Which
> will answer a lot of the questions you are about to
> ask.

I didn't find it that inspiring on a browse, but I'll check it out.

> If you try to do the "simplest thing that might
> possibly work." Then you would probably start with the
> ivars and switch over to using accessors when there
> becomes a need.

I guess that works pretty painlessly.

> Go reread the comment for #removeAll I believe you
> will find it does not do what you wish.

Actually, it does, as long as I create an identity copy of the collection  
to pass as the parm.

> Try:
>
> myCollection := OrderedCollection new
> add: 4
> ; add: 3
> ; add: 2
> ; add: 1
> ; yourself.
> myCollection removeAll: myCollection  .
>
> and see what you get. See if you can figure out why.

This looks like something I've seen often in other languages, where the  
iterator ends up missing items because it's changing as it goes. Hard for  
me to believe this is a bug, but that's what it looks like to me.

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