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

nb::handle: make bool conversion explicit to avoid ambiguity with integer conversions in derived types #173

Merged
merged 1 commit into from Apr 5, 2023

Conversation

oremanj
Copy link
Contributor

@oremanj oremanj commented Apr 5, 2023

An explicit operator bool() can still be used in conditional tests without a cast (if (h) { ... }) and is a better match for the explicitness of, for example, operator int() in nb::int_. Without this change, if you write

void fn(nb::int_ ival) {
    int i = ival;
    printf("%d\n", i);
}

then fn(nb::int_(42)) will print 1. (Only implicit conversions are allowed by the initialization style used, so the implicit operator bool in nb::handle is eligible while the explicit operator int in nb::int_ is not.)

…eger conversions in derived types

An `explicit operator bool()` can still be used in conditional tests without a cast (`if (h) { ... }`) and is a better match for the explicitness of, for example, `operator int()` in `nb::int_`. Without this change, if you write
```
void fn(nb::int_ ival) {
    int i = ival;
    printf("%d\n", i);
}
```
then `fn(nb::int_(42))` will print 1. (Only implicit conversions are allowed by the initialization style used, so the implicit `operator bool` in `nb::handle` is eligible while the explicit `operator int` in `nb::int_` is not.)
@wjakob
Copy link
Owner

wjakob commented Apr 5, 2023

Oh yes, let's have this. I am surprised it was not explicit to start with.

@wjakob wjakob merged commit c4be1b6 into wjakob:master Apr 5, 2023
16 checks passed
wjakob pushed a commit that referenced this pull request Apr 5, 2023
…eger conversions in derived types (#173)

An `explicit operator bool()` can still be used in conditional tests without a cast (`if (h) { ... }`) and is a better match for the explicitness of, for example, `operator int()` in `nb::int_`. Without this change, if you write
```
void fn(nb::int_ ival) {
    int i = ival;
    printf("%d\n", i);
}
```
then `fn(nb::int_(42))` will print 1. (Only implicit conversions are allowed by the initialization style used, so the implicit `operator bool` in `nb::handle` is eligible while the explicit `operator int` in `nb::int_` is not.)
@oremanj oremanj deleted the fix-int-cast branch May 1, 2023 00:29
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.

None yet

2 participants