newbie question on OrderedCollections

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

newbie question on OrderedCollections

P.S. Neeley
I'm working my way through 'On to Smalltalk' by Winston.   In Chapter 21 he
seems to expect (and I do too) that this piece of code:

OC := OrderedCollection new add: 640

would create a new OrderedCollection and add the integer 640 as a member,
but what really results is OC becomes an instance of the class SmallInteger.

Why?  Where have I gone wrong?

Best Wishes,

Steve


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

Ian Bartholomew
Steve,

> OC := OrderedCollection new add: 640
>
> would create a new OrderedCollection and add the integer 640 as a member,
> but what really results is OC becomes an instance of the class
SmallInteger.
>
> Why?  Where have I gone wrong?

Take it through a step at a time, remembering the Smalltalk precedence rules
and the fact that every Smalltalk message send answers another object as a
result of it's evaluation.

There are two message sends here.

The first sends #new to the class OrderedCollection and the answer is a new
instance of OrderedCollection.

The second sends #add: to this new instance of OrderedCollection but, and
this is the bit that can be confusing until you get used to it, the answer
from this message send is the object that was just added (640) and not the
object that received the message (the new OrderedCollection instance).

Finally, the assignment then sets OC to the object answered by the #add:
message i.e. 640.

There are three main ways around this

1) Create and add separately.
OC := OrderedCollection new.
OC add: 640

2) Use the #yourself message as part of a cascade
OC := OrderedCollection new
    add: 640;
    yourself

3) Use one of the shortcut constructor methods
OC := OrderedCollection with: 640

Ian

BTW This is almost identical to the first question I asked on an online
forum, about 15 years ago. It does get easier, honestly <g>


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

Keith Alcock
In reply to this post by P.S. Neeley
Steve,

"P.S. Neeley" wrote:
>
> I'm working my way through 'On to Smalltalk' by Winston.   In Chapter 21 he
> seems to expect (and I do too) that this piece of code:
>
> OC := OrderedCollection new add: 640
>
> would create a new OrderedCollection and add the integer 640 as a member,


It does.


> but what really results is OC becomes an instance of the class SmallInteger.
>


Even though OC is an integer, there is at least temporarily an ordered
collection with 640.


> Why?  Where have I gone wrong?
>


The ordered collection was not assigned to OC.  Try to find the return
value of the add: message.

You could also experiment with

(OC := OrderedCollection new) add: 640.

OC := OrderedCollection new.
OC add: 640.

OC := OrderedCollection new
        add: 640;
        yourself.


> Best Wishes,
>
> Steve


This had better not be a homework question.

Keith Alcock


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

Dave Harris-3
In reply to this post by Ian Bartholomew
[hidden email] (Ian Bartholomew) wrote (abridged):
> There are three main ways around this

A fourth is to use brackets:
   (OC := OrderedCollection new) add: 640.

  Dave Harris, Nottingham, UK | "Weave a circle round him thrice,
      [hidden email]      |   And close your eyes with holy dread,
                              |  For he on honey dew hath fed
 http://www.bhresearch.co.uk/ |   And drunk the milk of Paradise."


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

Ian Bartholomew
> A fourth is to use brackets:
>    (OC := OrderedCollection new) add: 640.

Well, it was a bit early <g>

There's (at least) one other way as well, and character wise it's shorter
than all the others!

5 bonus points for the first correct solution.

Ian


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

Aaron Wieland
> There's (at least) one other way as well, and character wise it's shorter
> than all the others!
>
> 5 bonus points for the first correct solution.

Hmmm....  Could it be:

OC := #(640) asOrderedCollection    ?

This is one character shorter than the #with: solution (but not quite as
readable, I'd say).

Cheers,
-- Aaron Wieland


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

Ian Bartholomew
> Hmmm....  Could it be:
>
> OC := #(640) asOrderedCollection    ?
>
> This is one character shorter than the #with: solution (but not quite as
> readable, I'd say).

That was the one I was thinking about, the 5 bonus points are yours <g>

I agree that it's not as readable but if you want to initialise an OC with a
number (> 5) of constant objects then it, or #withAll:, might be better than
a chain of #add:s

Ian


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

P.S. Neeley
In reply to this post by P.S. Neeley
Thank you for the many helpful responses.  I appreciate it.  No, this was
not a homework problem.  I actually am working through Winston's 'On to
Smalltalk', on my own using the Value Edition of Dolphin, as a VB programmer
seeking to expand his horizons.

Anyway,

I'm also stuck on Chapter 22 on File Streams.  I've created a file --
c:/testit.txt -- and would like to do Chapter 22 but cannot because Dolphin
differs from the book.  In particular, Winston sets up a File Stream as
follows:

inputStream := File pathname: 'testit.txt'

But this doesn't work in Dolphin.  What is the proper syntax to set up a
File Stream given a pathname?  I've searched the class hierarchy but
couldn't find anything that looked too hopeful.

Thanks,

Steve


P.S. Neeley <[hidden email]> wrote in message
news:Da4I6.5880$[hidden email]...
> I'm working my way through 'On to Smalltalk' by Winston.   In Chapter 21
he
> seems to expect (and I do too) that this piece of code:
>
> OC := OrderedCollection new add: 640
>
> would create a new OrderedCollection and add the integer 640 as a member,
> but what really results is OC becomes an instance of the class
SmallInteger.
>
> Why?  Where have I gone wrong?
>
> Best Wishes,
>
> Steve
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

Christopher J. Demers
P.S. Neeley <[hidden email]> wrote in message
news:j1lI6.368$[hidden email]...
> I'm also stuck on Chapter 22 on File Streams.  I've created a file --
> c:/testit.txt -- and would like to do Chapter 22 but cannot because
Dolphin
> differs from the book.  In particular, Winston sets up a File Stream as
> follows:
>
> inputStream := File pathname: 'testit.txt'
>

Look at class side of the FileStream class.  Based on the context above it
looks like you could use:

inputStream := FileStream read: 'testit.txt' text: true.

Or one of the other similar methods if you want read/write access.

Chris


Reply | Threaded
Open this post in threaded view
|

Re: newbie question on OrderedCollections

Ronald Hallam
In reply to this post by P.S. Neeley
Hi,
    The syntax is

    | instream |
    instream := FileStream open:'c:\data\test1titan.dat' mode: #read.

    The mode is #read ,#append , #truncate, etc.


    Ron


P.S. Neeley wrote in message ...
>Thank you for the many helpful responses.  I appreciate it.  No, this was
>not a homework problem.  I actually am working through Winston's 'On to
>Smalltalk', on my own using the Value Edition of Dolphin, as a VB
programmer
>seeking to expand his horizons.
>
>
>Steve
>


Reply | Threaded
Open this post in threaded view
|

... use the Source Luke !

Davorin Rusevljan-2
In reply to this post by P.S. Neeley
"P.S. Neeley" <[hidden email]> wrote in message
news:j1lI6.368$[hidden email]...
> But this doesn't work in Dolphin.  What is the proper syntax to set up a
> File Stream given a pathname?  I've searched the class hierarchy but
> couldn't find anything that looked too hopeful.

I see that you have received a number of helpfull answers allready, so I'll
in addition try to offer you a more general advice how to progress faster
(which by any means does not imply your questions are not welcommed).

My experience with learning the Smalltalk is that the I have learned the
most from the image. At the begining the size of the thing is ovehelming,
and programers like me tend to be used good old reference manuals things so
it is not an easy start. But it is definitely worthwile. When you would like
to know how to do something, dig through an image until you find example. At
the start you will be spending a lot of time because everything is new, but
in time it will become easy thing. Your good friends in doing so are class
browser and extremly usefull searches for Senders of the messages (methods
that invoke particuzlar method), searches for Receivers (ones that implement
the method), and searches for the methods containing some text.

Anyway, this image digging can help you in more than one way. Usually, you
will get what you were trying to find, but additional benefit is that you
will be reading a lot of good Smalltalk code which will help you to
understand what really makes Smalltalk tick, and how to write programs in
it.

Davcorin Rusevljan


Reply | Threaded
Open this post in threaded view
|

Re: ... use the Source Luke !

Olek
Dav,


>
>Anyway, this image digging can help you in more than one way.


I think you are absolutely right but sometimes it can be dangerous for
programmer health. I as absolute smalltalk beginner remember my image
digging ( just for fun ) about one week ago. I found quick sort
implementation and right after that how is PI const defined and was
quite ready to jump out of the window. :-)


Olek