johnlopez2000
Programmer
Below, I have 2 implementations showing an attempt to replicate a STRUCT(C) or TYPE(VB) in TCL.
IMP#1) eccentially looks to me to be an array of namespaces, whereas IMP#2) seems to be an array within a namespace.
Both implementations work. ::Object::init is called first, then each struct array element is populated with something similar to:
set x [::Object::addRecord "Mech Dwg" 300601 A]
One can view array elements as:
IMP#1) puts $::Object(3,REV)
IMP#2) puts $::Object::Obj(3,REV)
Even though both work, is IMP#1 invalid per TCL standards or styling? Will I get in trouble with memory requirements with #1 or #2?
Your opinions are appreciated. (see below)
#~~~~~~~# implementation #1 #~~~~~~~#
#~STRUCT object and methods~#
namespace eval Object {
proc init {} {
variable index
set index 0
}
proc incrIndex {} {
incr ::Object::index
}
proc addRecord { xType xName xRev } {
set ::Object($::Object::index,TYPE) $xType
set ::Object($::Object::index,NAME) $xName
set ::Object($::Object::index,REV) $xRev
set x [::Object::incrIndex]
}
}
#~~~~~~~# implementation #2 #~~~~~~~#
namespace eval Object {
proc init {} {
variable index
set index 0
}
proc incrIndex {} {
incr ::Object::index
}
proc addRecord { xType xName xRev } {
set ::Object::Obj($::Object::index,TYPE) $xType
set ::Object::Obj($::Object::index,NAME) $xName
set ::Object::Obj($::Object::index,REV) $xRev
set x [::Object::incrIndex]
}
}
IMP#1) eccentially looks to me to be an array of namespaces, whereas IMP#2) seems to be an array within a namespace.
Both implementations work. ::Object::init is called first, then each struct array element is populated with something similar to:
set x [::Object::addRecord "Mech Dwg" 300601 A]
One can view array elements as:
IMP#1) puts $::Object(3,REV)
IMP#2) puts $::Object::Obj(3,REV)
Even though both work, is IMP#1 invalid per TCL standards or styling? Will I get in trouble with memory requirements with #1 or #2?
Your opinions are appreciated. (see below)
#~~~~~~~# implementation #1 #~~~~~~~#
#~STRUCT object and methods~#
namespace eval Object {
proc init {} {
variable index
set index 0
}
proc incrIndex {} {
incr ::Object::index
}
proc addRecord { xType xName xRev } {
set ::Object($::Object::index,TYPE) $xType
set ::Object($::Object::index,NAME) $xName
set ::Object($::Object::index,REV) $xRev
set x [::Object::incrIndex]
}
}
#~~~~~~~# implementation #2 #~~~~~~~#
namespace eval Object {
proc init {} {
variable index
set index 0
}
proc incrIndex {} {
incr ::Object::index
}
proc addRecord { xType xName xRev } {
set ::Object::Obj($::Object::index,TYPE) $xType
set ::Object::Obj($::Object::index,NAME) $xName
set ::Object::Obj($::Object::index,REV) $xRev
set x [::Object::incrIndex]
}
}