Ummmm....ok?

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

Ummmm....ok?

Brody
I'm playing around with some code, and I was testing it via workspace
code.

|  x  y |
x := LABSingletonTester singleton.
x name: 'Larry'.
x address: 'blue'.



When I highlighted the code, and attempted to execute it, at the bottom
of the screen I got a red X and the message:

     "Error:  Cannot redefine shared variable  y"

Ok...what's this mean?  It's a temp variable.  It didn't complain about
the 'x' variable.  Who am I sharing variable 'y' with?

Me so confused.

Thanks,

Brody


Reply | Threaded
Open this post in threaded view
|

Re: Ummmm....ok?

Ian Bartholomew-19
Brody,

> Ok...what's this mean?  It's a temp variable.  It didn't complain about
> the 'x' variable.  Who am I sharing variable 'y' with?

This is actually the result of a very useful Dolphin feature, the ability to
use variables in a workspace without having to define them.  To
demonstrate...

If you select and evaluate the following in a _new_ workspace then it will
work  NB. This is just the same code as your original example but using a
generic class.

| x y |
x := Array new: 2.
x at: 1 put: 1.
x at: 2 put: 2.

Now in a __new__ workspace select and evaluate ...

x := Array new: 2.
x at: 1 put: 1.
x at: 2 put: 2.

It works with no errors even though you haven't defined x.  You can
inspect/access the x variable by evaluating "x inspect" or by using the
"Workspace/Variables" menu option.  This is because Dolphin has created
temporary variables for you.

Now, in the __same__ workspace as the last example, select and evaluate the
original code snippet

| x y |
x := Array new: 2.
x at: 1 put: 1.
x at: 2 put: 2.

You will get a warning about redefining the static variable x.  This is
because Dolphin has already created an x variable for you when you evaluated
the first example in the workspace.  Now you have explicitly defined another
x variable yourself and Dolphin is warning about the two variables clashing.


In your original posted example you must have, at some stage, used a
variable y without defining it and Dolphin had created it for you.  That is
why you are getting the warning.

To get around it you can either delete the variable declaration in your
code, delete the Dolphin created variables using the menu command or
cut/paste the code to a new workspace.

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.