Closed paulerikf closed 2 years ago
How should I be setting up a jluna usertype for something I already have a Julia side type for? Should I be making an equivalent type through the jluna usertype system and doing conversions Julia side (or using multiple dispatch when possible)? Or could I somehow link my jluna usertype to the existing Julia side struct?
Don't, if you already have a Julia-side type there is no point to using jluna::usertype. I think you misunderstood its purpose, it is specifically meant to create a not-yet-existing Julia-side type and make easy conversion possible in the C++ -> Julia direction. Everything else you should custom define.
If you already have a Julia-side type, simply write a wrapper like so:
# in julia
struct JuliaSideType
member::Any
JuliaSideType() = new(nothing)
end
julia_side_type_method(x::JuliaSideType) = println("do something");
// in cpp
class JuliaSideTypeWrapper
{
public:
// attach existing Julia-side value
JuliaSideTypeWrapper(jluna::Proxy& in)
: _value(in)
{
static auto* julia_side_type_type = Main["JuliaSideType"];
assert(in.isa(julia_side_type_type));
}
// newly allocate Julia-side instance
JuliaSideTypeWrapper()
{
static auto new_julia_side_type = Main["JuliaSideType"];
in = new_julia_side_type();
}
// allow for use with other proxy functions by allowing implicit conversion
operator jluna::unsafe::Value*()
{
return in.operator unsafe::Value*();
}
// wrap method
void julia_side_type_method()
{
jluna::Main["julia_side_type_method"](_value);
}
private:
jluna::Proxy _value;
}
(example untested, it's just to illustrate the design pattern)
Can I use a usertype in another usertype? Is there anything I need to do to make that possible? Currently getting the following error when trying to build the example below: In template: no member named 'type_name' in 'jluna::detail::as_julia_type_aux
'
You can, add the following before the declaration of JlunaHeader
:
namespace jluna::detail
{
template<>
struct as_julia_type_aux<JlunaStamp>
{
static inline const std::string type_name = "JlunaStamp";
};
}
It adds a case to the template meta function jluna::detail::as_julia_type_aux
so it works, It's mentioned in the error but I also realize that template meta functions aren't a very accessible thing, so I might make a macro for this since the error isn't very descriptive.
Rather than pasting the above mentioned code, you can now call make_usertype_implicitly_convertible(JlunaStamp)
once #37 is merged.
Hey clem, haven't been working with Jluna for a few weeks, but back at it.
Got a couple of quick question about usertypes best practices.
How should I be setting up a jluna usertype for something I already have a Julia side type for? Should I be making an equivalent type through the jluna usertype system and doing conversions Julia side (or using multiple dispatch when possible)? Or could I somehow link my jluna usertype to the existing Julia side struct?
Can I use a usertype in another usertype? Is there anything I need to do to make that possible? Currently getting the following error when trying to build the example below:
In template: no member named 'type_name' in 'jluna::detail::as_julia_type_aux<JlunaStamp>'