CxxWrap.jl icon indicating copy to clipboard operation
CxxWrap.jl copied to clipboard

How to wrap operators?

Open TransGirlCodes opened this issue 5 years ago • 1 comments

How do I wrap operators like:

LinkedReadMapper operator=(const LinkedReadMapper &other);

or

bool operator==(const SequenceGraph &o) const ;

My guess is something like: .method("=", operator=(const LinkedReadMapper &other)) and .method("==", operator==(const SequenceGraph &other)) or maybe "Base.=="?

But I wasn't sure if that was the correct way of doing it.

Thanks!

TransGirlCodes avatar Nov 07 '18 13:11 TransGirlCodes

For ==, something like that should work, using the function pointer, e.g. .method("==", &SequenceGraph::operator==).

The implementation for this is currently quite ugly, there is a predefined list of names that are automatically added to base: https://github.com/JuliaInterop/CxxWrap.jl/blob/master/src/CxxWrap.jl#L334-L342

I intend to change this in the future, by allowing to specify the module to which the method should be added using an optional argument. For now, for function not in the list you will have to define a function local to your module, and then overload the Base function as usual on the Julia side.

For = there is no way to do it: it cannot be overloaded in Julia, since assignment is just setting what value is pointed to by a given variable name. The best equivalent would be to overload copy!.

barche avatar Nov 07 '18 15:11 barche

@barche close?

fingolfin avatar Jan 05 '23 02:01 fingolfin

Yes, this is done now, using the set_override_module function (on the C++ side).

barche avatar Jan 05 '23 16:01 barche