Version 0 of cloneProc

Updated 2000-06-09 16:42:35

Believe it or not, I actually needed this for something!

 ## ******************************************************** 
 ##
 ## Name: cloneProc
 ##
 ## Description:
 ## Creates a new procedure identical in behaviour to a
 ## prototype procedure, but with a different name.
 ##
 ## Parameters:
 ##
 ## Usage:
 ##
 ## Comments:
 ##

 proc cloneProc { proto clone } {
      eval proc $clone \{ [ info args $proto ] \} \
                       \{ [ info body $proto ] \}
 }
 ## ******************************************************** 

-PSE


Hmmm, seems like the following is simpler:

 proc cloneProc { proto clone } {
     proc $clone [info args $proto] [info body $proto]
 }

I'm curious why you thought all the extra quoting was needed?

Because I always wear a belt and suspenders. -pse

Of course, both versions suffer from the inability to clone default arguments. An exercise for the next reader?

-DGP


Sure, I'll bite:

 proc cloneProc { proto clone } {
     set argv {}
     foreach x [info args $proto] {
         if {[info default $proto $x y]} {
             set x [list $x $y]
         }
         lappend argv $x
     }
     proc $clone $argv [info body $proto]
 }

-JC

DKF: Woe betide anyone trying to clone anything with a (admittedly bizarre) argument name with a space in with the above code:

  proc cloneProc {proto clone} {
      set argv [list]
      foreach x [info args $proto] {
          if {[info default $proto $x y]} {
              lappend argv [list $x $y]
          } else {
              lappend argv [list $x]
          }
      }
      proc $clone $argv [info body $proto]
  }

(careful Donal, you're about to step on my patent for cloning admittedly bizarre things in space)