matusnovak / wrenbind17

A header only library for binding C++17 classes and functions to Wren, an embeddable programming language
https://matusnovak.github.io/wrenbind17
MIT License
65 stars 10 forks source link

Binding multiple constructors from C++ class #12

Open llm96 opened 1 year ago

llm96 commented 1 year ago

Is it possible to bind multiple constructors with this library? I've tried doing it with the following sample code without success.

class test
{
    public:
        test(const std::string& arg1)
            { std::cout << std::format("{}\n", arg1); }
        test(const std::string& arg1, const std::string& arg2)
            { std::cout << std::format("{}, {}\n", arg1, arg2); }
};

int main(int argc, char* argv[])
{
    auto vm = wren::VM();
    auto& example = vm.module("example");
    auto& lib = example.klass<test>("test");

    lib.ctor<const std::string&>();
    lib.ctor<const std::string&, const std::string&>();

    try
    {
        vm.runFromSource("main", R"(
            import "example" for test

            var test1 = test.new("1")
            var test2 = test.new("1", "2")
        )");
    }
    catch (const std::exception& e)
        { std::cerr << e.what() << std::endl; }

    return 0;
}
Runtime error: test metaclass does not implement 'new(_)'.
  at: main:4
matusnovak commented 1 year ago

Unfortunately only one constructor can be specified at the moment.

Some alternatives that I can suggest: