Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I define a variable in another namespace?

64 views
Skip to first unread message

Juge

unread,
Sep 5, 2019, 4:30:29 AM9/5/19
to
I am working in one namespace, can I define a variable for another namespace?
I only saw some examples of nested namespaces, but what I want is kind of parallel namespaces.

I have something like:


namespace eval defaults {
variable justanothernamespace::variablethatwasdefinedelsewhere "Success"
} provide defaults 1

namespace eval justanothernamespace {
proc printthedamnvariable {} {
puts $variablethatwasdefinedelsewhere
}
} provide justanothernamespace 1


package require defaults
package require justanothernamespace

justanothernamespace::printthedamnvariable



I did not try this out yet so maybe it would even work maybe not. But I guess I am after something that is not so recommendable?

Harald Oehlmann

unread,
Sep 5, 2019, 6:01:46 AM9/5/19
to
You may use full qualified names to access variables in other namespaces
from anywhere:

% namespace eval ns1 {variable ns1var 0}
% namespace eval ns2 {puts $::ns1::ns1var}
0

Harald

Gerald Lester

unread,
Sep 5, 2019, 8:49:07 PM9/5/19
to
On 9/5/19 3:30 AM, Juge wrote:
> I am working in one namespace, can I define a variable for another namespace?
> I only saw some examples of nested namespaces, but what I want is kind of parallel namespaces.
>
> I have something like:
>
>
> namespace eval defaults {
> variable justanothernamespace::variablethatwasdefinedelsewhere "Success"
That should have been just:
set ::justanothernamespace::variablethatwasdefinedelsewhere "Success"
> } provide defaults 1
>
> namespace eval justanothernamespace {
> proc printthedamnvariable {} {
> puts $variablethatwasdefinedelsewhere
> }
> } provide justanothernamespace 1
>
>
> package require defaults
> package require justanothernamespace
>
> justanothernamespace::printthedamnvariable
>
>
>
> I did not try this out yet so maybe it would even work maybe not. But I guess I am after something that is not so recommendable?
>

As another poster said, just use fully qualified names.

--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Schelte

unread,
Sep 6, 2019, 6:51:53 AM9/6/19
to
On Thu, 05 Sep 2019 01:30:26 -0700, Juge wrote:
> I am working in one namespace, can I define a variable for another
> namespace?

You may want to take a look at [namespace upvar].


Schelte.

Donal K. Fellows

unread,
Sep 9, 2019, 9:42:31 AM9/9/19
to
On 05/09/2019 09:30, Juge wrote:
> I did not try this out yet so maybe it would even work maybe not.

It almost works. You need to create the namespace before you can set
values in it, you need to use [variable] inside the using procedure, and
you need to use the fully qualified name inside [variable] to do what
you're after safely. In short, this works (with some shorter names to
make things fit nicely on a line):

namespace eval jans {}

namespace eval defaults {
variable ::jans::vardefelse "Success"
}

namespace eval jans {
proc printvar {} {
variable vardefelse
puts $vardefelse
}
}

jans::printvar; # ==> Success

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.

Juge

unread,
Sep 11, 2019, 3:30:22 PM9/11/19
to
I wonder whether I can use the namespace eval several times? I have my program in packages and there I have the namespace eval and provide. My options routine kicks in before this at the very beginning of the program. So I am wondering whether I can use already there the suggested short command like:

namespace eval ns1 {variable ns1var 0}

and then later on again the namespace eval ns1 for the actual package? Yes, I could simply try it out, it will probably not melt down my PC or anything, but I just ask

and of course thank for all the answers until now...

Rich

unread,
Sep 11, 2019, 3:41:09 PM9/11/19
to
Yes. From the man page:

namespace eval namespace arg ?arg ...?
Activates a namespace called namespace and evaluates some code in
that context. If the namespace does not already exist, it is
created. If more than one arg argument is specified, the

Namespace eval is a "run this code in namespace X" command, with a side
effect of "create X if X does not already exist".

0 new messages