Re: can you return an object from a proc?



On Jun 24, 12:23 pm, googlegro...@xxxxxxxxxxxxxxx wrote:
On Jun 24, 5:45 am, chedderslam <chedders...@xxxxxxxxx> wrote:



In the above case, the name of the object is "db"; "db" is not a
variable, it is the name of an object. That is why you get the error.
You might try this instead:

     return db

That will work unless msado objects are freed when they go out of scope.
I have no idea how msado works, but you might give that a try.

Thank you for the help.  What you suggested seems to work.  What I
don't understand is how the interpreter knows that I am referring to
the object.  For instance, "set myvar db" would just set the variable
myvar to the string "db".  How does the return statement know I am
referencing the object, rather than just returning the string "db"?

Thanks.

This way of dealing with objects confused me at first too. So,
bear with me if I am a bit verbose in my explanation.

When you create your object you provided the string "db" to be
used to reference the object. But it also created a command
called db which you then used to configure it. This is similar
to how when you create a widget (for example, [button .b])
you also get a command created (in this case .b) which is used
to configure the widget.

Also note that in tcl, a string need not be in "quotes" if it
has no embedded spaces. So, in your case, "db" or just db do the same
thing in the object create call, msado db. This could have
been written as msado "db".

Same with the return db, which is the same as return "db".

So, it IS just returning the string db.

Note, you likely cannot call your procedure more than once, since
it creates an object called db each time. You would have to
destroy the db object first, and then you could create a new
one with the same name of db.

If I wrote this, it would be similar to what you have written,

% proc create_button {} {

 button .b
 .b configure -text name

 return .b
 }

And here's what happens if I call this twice without first
destroying the button object called .b

 % create_button
.b
 % create_button
window name "b" already exists in parent

This is not how most other object oriented languages
operate. They have constructors and destructors. When you
create an object in java, or c++, you are returned
a reference (normally a word sized address) which you
then use to manipulate the object. Tcl does this for
some objects, like opened files or sockets. But other
objects, like yours and like widgets, you have to provide
a unique text string to reference the object, and the
object creation call uses that string to reference the
object, AND creates a command of the same name that you
then use to manipulate the object.

It takes a bit getting used to. Tcl is a command
oriented language that can be made to look very
much like an object oriented language. But you
sometimes need to understand the inner workings.

Hope this helps.

Good information. Thank you.
.