Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding multiple constructors from C++ class #12

Open
llm96 opened this issue Mar 12, 2023 · 1 comment
Open

Binding multiple constructors from C++ class #12

llm96 opened this issue Mar 12, 2023 · 1 comment

Comments

@llm96
Copy link

llm96 commented Mar 12, 2023

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
Copy link
Owner

matusnovak commented Mar 15, 2023

Unfortunately only one constructor can be specified at the moment.

Some alternatives that I can suggest:

  • Binding a static function to the class to serve as the constructor
  • Creating a factory class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants