Skip to content

[cxx-interop] Add CxxOptional protocol for std::optional ergonomics #63683

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

Merged
merged 2 commits into from
Mar 6, 2023

Conversation

egorzhdan
Copy link
Contributor

This adds a protocol to the C++ standard library overlay which will improve the ergonomics of std::optional when used from Swift code.

As of now, the overlay adds an initializer of Swift.Optional that takes an instance of CxxOptional as a parameter.

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Feb 15, 2023
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-optional branch from 029f75b to e3acf24 Compare February 15, 2023 19:27
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

}

extension Optional {
public init<O: CxxOptional>(fromCxx value: O) where O.Wrapped == Wrapped {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lorentey Karoy, could you please take a look to see if the signature of this initializer looks reasonable, specifically the fromCxx label?
I couldn't make this initializer unlabeled, since Swift.Optional already has an unlabeled initializer that wraps around the passed object, which isn't what we'd like to happen here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks okay! Naming is difficult as usual, but fromCxx is plausible, and if we figure out a better convention, keeping this one around will not hurt much.

@egorzhdan egorzhdan requested a review from lorentey February 15, 2023 19:34
}

extension CxxOptional {
public var hasValue: Bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property provides the only way to check if the std::optional has a value from Swift.

@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-optional branch from e3acf24 to 6672cd3 Compare February 16, 2023 17:54
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with some comments! 👍

This adds a protocol to the C++ standard library overlay which will improve the ergonomics of `std::optional` when used from Swift code.

As of now, the overlay adds an initializer of `Swift.Optional` that takes an instance of `CxxOptional` as a parameter.
@egorzhdan egorzhdan force-pushed the egorzhdan/cxx-optional branch from 6672cd3 to a12986a Compare March 3, 2023 18:41
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test macOS

@egorzhdan egorzhdan merged commit ac72084 into main Mar 6, 2023
@egorzhdan egorzhdan deleted the egorzhdan/cxx-optional branch March 6, 2023 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants