Search code examples
hidetclaliasinterpreterslave

Use tcl interp alias to rename tcl built in command


How can I rename an existing tcl command in a slave interpreter?

In other words

interp create test
test alias __proc proc

test eval {
  __proc hello {} {
    puts "hiya"
  }
  hello
}

This unfortunately does not work. However, if I hide and expose under a different name that works. But, I would like to use both commands proc and __proc - so I would prefer to use aliases, or any other way...


Solution

  • The alias command on a Tcl interpreter allows you to create a command in an interpreter that causes code to run in a different interpreter. So that's not what you want.

    I think the following does what you want:

    interp create test
    
    test eval {
      rename proc __proc
    
      __proc hello {} {
            puts "hiya"
      }
    
      hello
    }
    

    You can then combine the creation of the interpreter with the renaming of the proc command as follows:

    proc myinterp {interpName newProcName} {
       interp create $interpName
       $interpName eval "rename proc $newProcName"
    }
    
    myinterp test func
    
    test eval { func greet {} { puts "hello" } }
    test eval ( greet }