Coder Social home page Coder Social logo

Usertype best practices? about jluna HOT 3 CLOSED

clemapfel avatar clemapfel commented on May 18, 2024
Usertype best practices?

from jluna.

Comments (3)

Clemapfel avatar Clemapfel commented on May 18, 2024

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)

from jluna.

Clemapfel avatar Clemapfel commented on May 18, 2024

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.

from jluna.

Clemapfel avatar Clemapfel commented on May 18, 2024

Rather than pasting the above mentioned code, you can now call make_usertype_implicitly_convertible(JlunaStamp) once #37 is merged.

from jluna.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.