SWIG TCL : Renaming the class constructor name from new_* to create_*

Go To StackoverFlow.com


Can we rename constructors like we rename functions using the %rename directive?

%rename(create_cell) Cell(string);

Basically, I want to end up with something like create_cell instead of new_Cell.

2012-04-04 06:31
by balaji kommineni


I suspect that you can't at that point (did you try it to see if it works?) but there are a few things you can do. (Only do one of them, of course.)

  1. Edit the generated code (SWIG writes the C++–Tcl binding code that is then compiled) so that the string "new_Cell" is "create_cell". I think you should be able to find the place to change in an argument a function call like Tcl_CreateCommand or Tcl_CreateObjCommand, but might also be in a macro depending on how the code generation is done. (I've never actually looked.)
  2. Use load to get the code into Tcl and then rename the command afterwards. Names are not fixed in stone. The load might be inside the implementation of a call to package require; just do what you would normally do to get the code working with the wrong name first, and then do this:

    rename new_Cell create_cell
  3. Add a wrapper command or procedure; any of these will do:

    proc create_cell args {
        eval new_Cell $args
    # With 8.5 or later
    proc create_cell args {
        new_Cell {*}$args
    # With 8.6
    proc create_cell args {
        tailcall new_Cell {*}$args
    # Or do this; not a procedure, an alias
    interp alias  {} create_cell  {} new_Cell
2012-04-04 07:04
by Donal Fellows
If you're using a wrapper or alias, you can also use rename to move the wrapped new_Cell to another Tcl namespace where it won't interfere so much with what you're seeing. Purely cosmetic, but convenient. (Myself, I'd wrap everything because I don't really like the interface style that SWIG produces; Tcl isn't C++ and its APIs have their own flavor. - Donal Fellows 2012-04-04 07:07
Thanks Donal, I think I will just rename the proc is TCL. That seems to be the simplest thing to do - balaji kommineni 2012-04-04 15:39