DarioP
DarioP

Reputation: 5465

Improve the Swig-tcl wrapping of a make function

When wrapping a constructor like:

struct A {
  A(){}
  void hello() {std::cout << "hello\n";}
};

Swig-tcl is amazing! Indeed in tcl I can just do:

A a
a hello

However there are cases where the creation of the object a is not straightforward (i.e. I may just want to return a reference to another object on the heap) and has to go through a make function like:

A make(){ return A(); }

then the wrapping of Swig-tcl becomes quite a mess:

set a [make]
$a hello

I am wondering if there is a way to restore the initial clarity of the tcl script when using a make function.

Upvotes: 0

Views: 51

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137667

I don't think it's particularly messy! It's just that the command in the second case has a name that you don't control, so you're keeping the name in a variable.

You can either rename the object command or make an alias to it:

rename $a a
interp alias {} a {} $a

After using either of those techniques, you'll have a command called a which you can invoke methods on. However, in both cases, if you're passing the object as an argument to another SWIGged function or method, you need the original name. Because of that, I'd personally advise just using the version with the name in a variable.

You should check that the version with rename works; it's possible — though unlikely — that it might not…

Upvotes: 1

Related Questions