JuliaInterop / CxxWrap.jl

Package to make C++ libraries available in Julia
Other
418 stars 67 forks source link

`@cxxdereference` fails to create a union when types are parametric #262

Closed rgcv closed 4 years ago

rgcv commented 4 years ago

When applied to functions whose parameter types might be themselves parametric, it will not recreate the function signature with a union of types. Take the following example:

#include <jlcxx/jlcxx.hpp>

template<typename T>
struct MyParametricType {};
JLCXX_MODULE define_julia_module(jlcxx::Module& mod) {
  mod.add_type<jlcxx::Parametric<jlcxx::TypeVar<1>>>("MyParametricType")
     .apply<MyParametricType<int_t>, MyParametricType<double>>([](auto wrapped) {});
}
module M
using CxxWrap
@wrapmodule ...
__init__() = @initcxx

@cxxdereference f(::MyParametricType) = 42
end

If we now inspect the output of methods(f), we find something like the following:

julia> import M

julia> methods(M.f)
[1] f(::MyParametricType) in Main at M:1

The expected behavior would be to generate a Union of all possible value, reference, and smart pointer types for type MyParametric (e.g. Union{MyParametricType,CxxBaseRef{MyParametricType},SmartPtr{MyParametricType}).

Additionally, it would be interesting to explore the capability of, instead just the raw abstract type, include the parameter type, be it a generic type T or a concrete type. As a hypothetical, ake the following as a potential example:

julia> @cxxdereference g(::MyParametricType{T}) where T = 42
g (generic function with 1 method)

julia> methods(g)
[1] f(::Union{MyParametricType{T},CxxBaseRef{MyParametricType{T}},SmartPtr{MyParametricType{T}}) where T in Main at CxxWrap.jl:787

Currently, this is unfeasible since T falls into the macro as an undefined symbol. There is most likely a way of circumventing this by defining reference_type_union for parametric types, but I'm not so sure. The reason why I argue including the parametric type might be useful is because it is added information one may rely upon (I have a use case for this in which it helps that I know the parametric type within the function to get added information. Specifically, I recently mapped Voronoi_diagram_2 from CGAL as a parametric type, which takes in a triangulation. I'd like to know which triangulation I'm leading with to see what type of Point_2 it uses).

barche commented 4 years ago

Should be fixed now, there was also a problem with inheritance and pointers and references.