newbie questionS

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

newbie questionS

SerGioGio
hi...
I'm new to Smalltalk...

I have several questions I haven't found the answer in web's tutorials:

1) the Image : as far as I have understood, the image represents the state
of the system (objects and classes). it means that if I create a class, or
an object , it will be saved in the image.
let's say I create a SmallInteger with
    test := 3.
I can release the object by saying (I guess)
    test := nil.

let's say however I don't do the previous line... the SmallInteger 3 will be
saved in the image... but what if I don't remember the name of the variable
I used to point it (ie #test) ? will it pollute the system undefinitely ? or
is there a way to find the name again ?

2) when I say
SmallInteger allInstances.
I get #()... even if I do
    test := 3.
...
why so ? allInstances should return all instances of SmallIntegers
created... test is one SmallInteger, as
    test class.
proves.

3) I start Dolphin, a workspace... if my first query is
    LargeInteger allInstances.
it will return dozens of instances. why so ? I have created nothing... this
means that there are instances of LargeInteger in the image... but then
where are they, what are their names ? (cf Q 1)
very surprisingly... they're is one instance of
    200 factorial.
in the collection returned.

4) I used not to program with a garbage collector, but to delete all objects
by myself...
I tried to play a bit with Playground example (derivates from View).
I created a new Playground :
    playground := Playground new.
the Playground returned by 'Playground new' is useful, because #playground
refers to it (I guess that's what is implemented in garbage collection ?) so
it will not be destroyed.
indeed,
    Playground allInstances.
returns a collection of 1 object.
but then, if I do :
    Playground new.
the Playground returned should be considered useless (not named)... so it
should be destroyed just after having been created.
However,
    Playground allInstances.
returns a collection of 2 objects.
why so ?

5) political question
so far, I 'm really impressed by smalltalk.
but can it really be used for software developpement,
or is it just a pure OO language to use for some experimentation before
coding with more pragmatic languages like C++ ?

Thanks in advance...
And sorry for my english !

SerGioGio


Reply | Threaded
Open this post in threaded view
|

Re: newbie questionS

Andy Bower
SerGioGio,

> 1) the Image : as far as I have understood, the image represents the state
> of the system (objects and classes). it means that if I create a class, or
> an object , it will be saved in the image.

That's right.

> let's say I create a SmallInteger with
>     test := 3.
> I can release the object by saying (I guess)
>     test := nil.
>
> let's say however I don't do the previous line... the SmallInteger 3 will
be
> saved in the image... but what if I don't remember the name of the
variable
> I used to point it (ie #test) ? will it pollute the system undefinitely ?
or
> is there a way to find the name again ?

Okay, SmallIntegers are not the best objects to choose but we'll come to
that in a minute. When you assign to variable the compiler does one of two
things.

a) If the variable name begins with a capital letter ("Test") then it will
prompt to ask you if you want to create a system global. If you say yes then
a new entry will be made in the Smalltalk global dictionary. Try inspecting
"Smalltalk" and you'll see what globals are already present; most will be
classes.

b) If the variable name begins with a lowercase letter ("test") as in your
example then the compiler will automatically generate a "workspace
variable". This is a private variable attached to, and known only by, the
Workspace that you are currently working in. The variable will remain around
until you close the workspace, after which it will be garbage collected. In
Dolphin 3.x or 4.x you can see what workspace variables are present using
the Workspace/Variables menu command.

> 2) when I say
> SmallInteger allInstances.
> I get #()... even if I do
>     test := 3.
> ...
> why so ? allInstances should return all instances of SmallIntegers
> created... test is one SmallInteger, as
>     test class.
> proves.

SmallIntegers are a special case in all (?) Smalltalks because they are used
so frequently. SmallIntegers are not allocated as individual objects but,
rather, are held within the object pointer. The VM knows that they are not
really object pointers because the lower bit is set indicating that the rest
of the pointer is to be interpreted as an integer. This is why (on a 32 buit
machine) SmallIntegers are only 31 bits in size. It also means that the
image "effectively contains all SmallIntegers" already since the space is
reserved for them in the object table. It wouldn't make much sense for
#allInstances to answer a collection containing evey possible SmallInteger
so they are omitted from the reply.

Try repeating your tests with another class of object, say Point or
Rectangle.

Point allInstances size "Display it"
test := 15@15 "Or Point new"
Point allInstances size "Display it"

Now close the workspace and open a new one to try:
Point allInstances size "Display it"
>
> 3) I start Dolphin, a workspace... if my first query is
>     LargeInteger allInstances.
> it will return dozens of instances. why so ? I have created nothing...
this
> means that there are instances of LargeInteger in the image... but then
> where are they, what are their names ? (cf Q 1)
> very surprisingly... they're is one instance of
>     200 factorial.
> in the collection returned.

Not all objects are named, many are anonymous. For example, if you evaluate:

myRect := Rectangle center: 0@0 extent: 50@50.

this creates a new instance of Rectangle and adds it to the workspace
variables dictionary with the name "myRect". As far as the rectangle itself
is concerned it has no idea what its name is. The same rectangle can also be
added to another dictionary and given a different name without interfering
with the first. This is fundamental to Smalltalk, in other languages it's as
if all objects are passed by reference. Anyway, rectangle that you created
also contains two anonymous Points (for the top left corner and extent) that
you haven't explicitly given a name to.

> 4) I used not to program with a garbage collector, but to delete all
objects
> by myself...
> I tried to play a bit with Playground example (derivates from View).
> I created a new Playground :
>     playground := Playground new.
> the Playground returned by 'Playground new' is useful, because #playground
> refers to it (I guess that's what is implemented in garbage collection ?)
so

> it will not be destroyed.
> indeed,
>     Playground allInstances.
> returns a collection of 1 object.
> but then, if I do :
>     Playground new.
> the Playground returned should be considered useless (not named)... so it
> should be destroyed just after having been created.
> However,
>     Playground allInstances.
> returns a collection of 2 objects.
> why so ?

A Playground is a window. All windows are implicitly held onto by the
operating system until they are closed. If you close the window then you'll
find that the object associated with it gets GC'd.

> 5) political question
> so far, I 'm really impressed by smalltalk.
> but can it really be used for software developpement,
> or is it just a pure OO language to use for some experimentation before
> coding with more pragmatic languages like C++ ?

Of course it can. Otherwise we wouldn't be here. It is much faster to code
in Smalltalk, the code is more easily readable and easier to change. It is
easier to manage complexity and build large applications in Smalltalk than
in C++. I suppose the question should be, what technical reasons are there
*not* to program in ST (there are some but not that many).

Best Regards,

Andy Bower
Dolphin Support
http://www.object-arts.com

---
Visit the Dolphin Smalltalk WikiWeb
http://www.object-arts.com/wiki/html/Dolphin/FrontPage.htm
---