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

Support nb::init(<lambda>) as syntactic sugar for custom constructors #885

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

oremanj
Copy link
Contributor

@oremanj oremanj commented Jan 28, 2025

Unlike the corresponding pybind11 feature, the lambda must return by value, not by pointer. This simplifies the plumbing quite a bit and allows us to typically take advantage of guaranteed copy elision, producing basically the same code that we would have gotten from the previously-recommended manual __init__ approach. Manual __init__ is still the most appropriate method if some action needs to be taken after calling the C++ constructor.

This PR fully supports alias/trampoline classes, including the option to pass separate factory functions for constructing the bound type vs the trampoline type, except that I didn't bother providing an equivalent of pybind11's init_alias for forcing to always construct the trampoline type (since we don't have that for C++-provided constructors either). If this PR is judged too complex, the easiest way to simplify it would be by dropping support for trampoline classes. I'm a bit reluctant to do that though, since there is no supported API for constructing a trampoline class using the manual approach (since detail::nb_inst_python_derived() is internal).

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

Successfully merging this pull request may close these issues.

1 participant