Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7714957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:15:56+00:00 2026-06-01T02:15:56+00:00

I am wrestling with trying to implement a simple object-oriented like system in Tcl

  • 0

I am wrestling with trying to implement a simple object-oriented like system in Tcl 8.4.18. I’ve looked at Itcl, stooops, XOTtcl, etc… and haven’t decided if I want to use them, especially if I can do it another simple way. Anyway, lets says I have a namespace

namespace eval Object {
    namespace export setvar

    proc setvar { model name value } {
        set ${model}::${name} $value
    }
}

and then I “subclass” if with another namespace

namespace eval Model {
    namespace import ::Object::*
    variable foo 0
}

I can set the variable like this

Model::setvar Model foo 2
puts $Model::foo

which outputs “2”. However, I would like to simplify the code so that the routine “setvar” from the Object namespace can determine that it is being called from the “Model” namespace. Something like this:

proc setvar { name value } {
    set myspace [namespace current]
    set ${namespace}::${name} $value
}

and then call it like

Model::setvar foo 2

but that doesn’t work because [namespace current] returns “::Object” and not “::Model”. According to the documentation that is because the importation just makes a reference back to the Object namespace.

The reason to use the routine setvar is to try and implement a variable override so that I can use foo from Model if it exists, otherwise get it from Object.

Are there any other methods to do this? Or should I just use one of the other tools?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:15:58+00:00Added an answer on June 1, 2026 at 2:15 am

    The code you are looking for is tricky, but possible to do (the key is namespace which):

    proc setvar { name value } {
        set cmdnameScript [list namespace which [lindex [info level 0] 0]]
        set myspace [namespace qualifiers [uplevel 1 $cmdnameScript]]]
        set ${myspace}::${name} $value
    }
    

    If you’re using 8.5 (or later), consider replacing that last set with:

    namespace upvar $myspace $name var
    set var $value
    

    It’s a lot easier to work with once things start to get complicated. Failing that, do use upvar (almost as easy to work with, not as fast, portable back as far as Tcl 8.0):

    upvar 0 ${myspace}::$name var
    set var $value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wrestling with something that should be very simple - specify a sidebar at
I was wrestling with mysql administrator trying to change the primary key on a
We have been wrestling with the confines of ASP.NET trying to get JSONP working.
I've been wrestling with this code for a while, trying to clean it up
in the moment im wrestling with core plot, and I´m trying to achieve that
I have been wrestling with what should be a very simple thing for weeks
I have been wrestling with SvcUtil all day, trying to get it to generate
been wrestling with this for some time. I am trying to access a REST
This should be simple enough, but after wrestling with it for hours, I still
So, I had need of a very, very simple image slider. Instead of wrestling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.