JuliaInterop / CxxWrap.jl

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

strictly typed constructors #200

Open kalmarek opened 4 years ago

kalmarek commented 4 years ago

Is it possible to make constructors striclty typed?

mod.add_type<my::Rational>("Rational",
                                jlcxx::julia_type("Real", "Base"))
        .constructor<int64_t, int64_t>()
        .constructor<my::Integer, my::Integer>()
        (...)
    );

However I'd like to channel julia constructor of Mod.Rational(num::Base.Integer, den::Base.Integer) through Mod.Integer (which can handle e.g. Mod.Integer(::BigInt) correctly), i.e. Mod.Rational(x::Base.Integer, y::Base.Integer) = Mod.Rational(Mod.Integer(BigInt(x)), Mod.Integer(BigInt(y))). At the moment I get WARNING: Method definition (::Type{Mod.Rational})(Integer, Integer) in module Mod at /home/kalmar/.julia/packages/CxxWrap/lDNAy/src/CxxWrap.jl:552 overwritten at /home/kalmar/.julia/dev/Mod/src/rationals.jl:4.

Unfortunately I can not add .constructor<jlcxx::StrictlyTypedNumber<int64_t>, jlcxx::StrictlyTypedNumber<int64_t>>() as

/home/kalmar/.julia/packages/CxxWrap/lDNAy/deps/usr/include/jlcxx/module.hpp:123:16: error: no matching function for call to ‘my::Rational(jlcxx::StrictlyTypedNumber<long int>&, jlcxx::StrictlyTypedNumber<long int>&)’

I could create mod.method("new_rational_si_si", [](jlcxx::StrictlyTypedNumber<int64_t> num, jlcxx::StrictlyTypedNumber<int64_t> den) {return my::Rational(num, den); });, but maybe there's a better way of doing this?

Sorry for the influx of issues.

barche commented 4 years ago

No worries, heep the issues coming ;)

This is another use case I didn't think of, I'd suggest going for a separate method using StrictlyTypedNumber, as you proposed. Ideally this should also work in the constructor, shouldn't be to difficult to add.