|
2015-04-25
| ||
| 21:06 | • Closed ticket [98217d03e0]: Add option to "namespace import" command f... artifact: ac3a015ead user: msofer | |
| 20:54 | • Ticket [98217d03e0]: 3 changes artifact: 17bb03e5e2 user: anonymous | |
| 20:41 | • Ticket [98217d03e0]: 3 changes artifact: 23bdec5294 user: anonymous | |
| 17:10 | • Pending ticket [98217d03e0]. artifact: 87d46a9b91 user: msofer | |
| 16:56 | • New ticket [98217d03e0]. artifact: f47cd01c96 user: anonymous | |
Add option to "namespace import" command for importing also variables
Closed98217d03e0c0032a72590b0f6af71b33d46494c6Created 12 years agoUpdated 12 years ago
Descriptionplain2015-04-25 16:56:27
Imagine this situation: You have a nested namespace with a lot defined constants. You want to import all these constants or some of them, because you don't want to write constantly the long namespace path to these constants. The command "namespace import" can only import procedures. But it can't import variables. My proposal is to add an option to "namespace import", for example "namespace import -variable", for importing variables of another namespace.
msoferwiki2015-04-25 17:10:16
I believe "namespace upvar" does most if not all of what you want. See http://tcl.tk/man/tcl8.6/TclCmd/namespace.htm#M21
anonymousplain2015-04-25 20:41:08
Hi Miguel,
now I am using the next trick. But don't you think that it's more concise something like this "namespace import -variable moduleA::moduleB::moduleC::moduleD::constants"?
This is the trick:
proc import_constants {path_to_constants variable_name args } {
upvar $variable_name _variable_name
upvar $path_to_constants _path_to_constants
if { [llength $args] > 0} {
foreach arg $args {
set _variable_name($arg) [dict get $_path_to_constants $arg]
}
} else {
array set _variable_name $_path_to_constants
}
}
namespace eval moduleA::moduleB::moduleC::moduleD {
variable constants [ dict create {*}{
constant1 value1
constant2 value2
constant3 value3
}]
}
import_constants moduleA::moduleB::moduleC::moduleD::constants constants
puts $constants(constant1)
anonymousplain2015-04-25 20:54:44
It's true. "namespace upvar" is all that I need.
namespace eval moduleA::moduleB::moduleC::moduleD {
variable constants {
constant1 value1
constant2 value2
constant3 value3
}
}
namespace upvar ::moduleA::moduleB::moduleC::moduleD constants constants_str
array set constants $constants_str
puts $constants(constant1)
msoferplain2015-04-25 21:06:19
Great! Closing the ticket then.
Log in (anonymous is fine) to add a comment.