Collection subclasses

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

Collection subclasses

Rob Rothwell
If I create a simple object subclassed from Collection, SequenceableCollection, or ArrayedCollection called CollectionObject, with

CollectionObject>>#initialize
     self halt.

and execute:

CollectionObject new.

the halt is executed.

Whereas, if I subclass CollectionObject from:

Set
Dictionary
OrderedCollection
Array

It does not.

Is one not supposed to make subclasses of these classes, and if so, why not?

I have an object that is most naturally a Dictionary-like object with various subclasses, but I can not initialize it...

Thank you; I feel I am missing something very basic in my understanding!

Rob Rothwell




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

Re: Collection subclasses

Randal L. Schwartz
>>>>> "Rob" == Rob Rothwell <[hidden email]> writes:

Rob> Is one not supposed to make subclasses of these classes, and if so, why not?

Generally, your object "has a" collection, but doesn't qualify to be "is a"
collection.  If you really want to subclass collection classes, you need
to peer deeply inside the existing classes, and do the right thing.

It's much easier to implement your object, throw a collection inside, then
implement the parts of the collection protocol that you end up using, starting
with #do:, by delegating those to your inside collection.

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

Re: Collection subclasses

Rob Rothwell
On Wed, Apr 2, 2008 at 10:48 PM, Randal L. Schwartz <[hidden email]> wrote:
>>>>> "Rob" == Rob Rothwell <[hidden email]> writes:

Rob> Is one not supposed to make subclasses of these classes, and if so, why not?

Generally, your object "has a" collection, but doesn't qualify to be "is a"
collection.  If you really want to subclass collection classes, you need
to peer deeply inside the existing classes, and do the right thing.

It's much easier to implement your object, throw a collection inside, then
implement the parts of the collection protocol that you end up using, starting
with #do:, by delegating those to your inside collection.

Thanks...I can accept that for now!

So the behavior I am seeing is to be expected, then?  And how would I have known to expect this?  I guess my assumption was that any object could "extend" any other object!

Rob


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

Re: Collection subclasses

Bert Freudenberg

On 03.04.2008, at 04:52, Rob Rothwell wrote:

> On Wed, Apr 2, 2008 at 10:48 PM, Randal L. Schwartz <[hidden email]
> > wrote:
> >>>>> "Rob" == Rob Rothwell <[hidden email]> writes:
>
> Rob> Is one not supposed to make subclasses of these classes, and if  
> so, why not?
>
> Generally, your object "has a" collection, but doesn't qualify to be  
> "is a"
> collection.  If you really want to subclass collection classes, you  
> need
> to peer deeply inside the existing classes, and do the right thing.
>
> It's much easier to implement your object, throw a collection  
> inside, then
> implement the parts of the collection protocol that you end up  
> using, starting
> with #do:, by delegating those to your inside collection.
>
> Thanks...I can accept that for now!
>
> So the behavior I am seeing is to be expected, then?  And how would  
> I have known to expect this?  I guess my assumption was that any  
> object could "extend" any other object!

You can, it just means it is not necessarily a good idea to do so.

And also, since Squeak is dynamically typed, you do not need to  
subclass to have your class be a drop-in replacement for some other  
class. Simply implement the methods that the other class uses, and fine.

- Bert -


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

Re: Collection subclasses

Michael Davies-2
In reply to this post by Rob Rothwell
On Thu, Apr 3, 2008 at 3:52 AM, Rob Rothwell <[hidden email]> wrote:
>  So the behavior I am seeing is to be expected, then?  And how would I have
> known to expect this?  I guess my assumption was that any object could
> "extend" any other object!
>

Hi Rob,

I wonder if you were thinking that #initialize was automatically
called on instantiation (like a constructor method in Java)? If that's
the case, this link may help:
http://onsmalltalk.com/programming/smalltalk/objects-classes-and-constructors-smalltalk-style/

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

Re: Collection subclasses

Nicolas Cellier-3
In reply to this post by Rob Rothwell
Rob Rothwell a écrit :

> If I create a simple object subclassed from Collection,
> SequenceableCollection, or ArrayedCollection called CollectionObject, with
>
> CollectionObject>>#initialize
>      self halt.
>
> and execute:
>
> CollectionObject new.
>
> the halt is executed.
>
> Whereas, if I subclass CollectionObject from:
>
> Set
> Dictionary

If you examine the protocol of these classes, you will see that they use
an #initialize: with an argument instead.

Look on the class side methods:

Set class>>new: nElements
        "Create a Set large enough to hold nElements without growing"
        ^ self basicNew initialize: (self sizeFor: nElements)

> OrderedCollection

Yet another implementation of new: ... basicNew setCollection:

> Array

No extra initialization at all performed by default (it's a deliberate
optimization).

> It does not.
>
> Is one not supposed to make subclasses of these classes, and if so, why not?
>
> I have an object that is most naturally a Dictionary-like object with
> various subclasses, but I can not initialize it...
>

Nothing prevents you to define

MyClass class>>new: n
        ^self basicNew initialize: n

Or the message you want...
But yes, initialize is NOT always sent upon instance creation.

> Thank you; I feel I am missing something very basic in my understanding!
>
> Rob Rothwell
>

You can of course create your own subclass (only SmallInteger is really
hardly subclassable if I remember).

Whether or not you should do it is another question...

Nicolas

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

Re: Re: Collection subclasses

Rob Rothwell
On Thu, Apr 3, 2008 at 3:47 PM, nicolas cellier <[hidden email]> wrote:
If you examine the protocol of these classes, you will see that they use an #initialize: with an argument instead.
No extra initialization at all performed by default (it's a deliberate optimization).

Ok...I kind of got the #initialize: n, but I guess I somehow thought that #initialize would be called as well (which doesn't make sense, I know)
 
You can of course create your own subclass (only SmallInteger is really hardly subclassable if I remember).

Whether or not you should do it is another question...

This is INDEED the question!

Thank you all.  I learned a lot from this little exercise...

Rob


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

Re: Collection subclasses

stephane ducasse
In reply to this post by Rob Rothwell
normally initialize instance side method is automatically invoked when  
you send  the message new.
Now for some collections this behavior has been optimized because it  
was not doing what it should do (unnnecessary initialization).

Stef

On Apr 3, 2008, at 4:31 AM, Rob Rothwell wrote:

> If I create a simple object subclassed from Collection,  
> SequenceableCollection, or ArrayedCollection called  
> CollectionObject, with
>
> CollectionObject>>#initialize
>      self halt.
>
> and execute:
>
> CollectionObject new.
>
> the halt is executed.
>
> Whereas, if I subclass CollectionObject from:
>
> Set
> Dictionary
> OrderedCollection
> Array
>
> It does not.
>
> Is one not supposed to make subclasses of these classes, and if so,  
> why not?
>
> I have an object that is most naturally a Dictionary-like object  
> with various subclasses, but I can not initialize it...
>
> Thank you; I feel I am missing something very basic in my  
> understanding!
>
> Rob Rothwell
>
>
>
> _______________________________________________
> 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: Collection subclasses

Rob Rothwell
On Mon, Apr 7, 2008 at 1:26 PM, stephane ducasse <[hidden email]> wrote:
normally initialize instance side method is automatically invoked when you send  the message new.
Now for some collections this behavior has been optimized because it was not doing what it should do (unnnecessary initialization).

Thanks...I mean, I could have figured out how to make something work, I just wanted to understand what was going on better to help gain a better understanding of the system!

Rob

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