Skip to content

Binding multiple constructors from C++ class #12

Description

@llm96

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions