Skip to content

Conversation

@Robertorosmaninho
Copy link
Contributor

This PR introduces the swift::Expected class.
The main idea is to have another approach to handling Swift Errors in C++ besides the already implemented throwing/catching exceptions.

@hyp hyp added the c++ interop Feature: Interoperability with C++ label Nov 1, 2022
@hyp
Copy link
Contributor

hyp commented Nov 1, 2022

This needs some testing. You can add a Cpp interop test that just tries to use this class with some values.

@Robertorosmaninho Robertorosmaninho force-pushed the interop/ExpectedClassForErrorHandling branch from ee27a92 to bbe669d Compare November 2, 2022 20:49
@Robertorosmaninho Robertorosmaninho marked this pull request as ready for review November 5, 2022 21:42
@Robertorosmaninho Robertorosmaninho requested review from hyp and removed request for egorzhdan and zoecarver November 5, 2022 21:42
Copy link
Contributor

Choose a reason for hiding this comment

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

The destructor should destroy the value or the error.

Copy link
Contributor

Choose a reason for hiding this comment

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

this is noexcept.

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be noexcept.

Copy link
Contributor

Choose a reason for hiding this comment

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

The parameter should probably be const T &val.

Copy link
Contributor

Choose a reason for hiding this comment

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

the move assignment should be deleted as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we check has_value explicitly like in value instead of specifying the precondition in the comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes!

Copy link
Contributor Author

@Robertorosmaninho Robertorosmaninho Nov 9, 2022

Choose a reason for hiding this comment

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

if it doesn't have a value can we abort?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes.

Copy link
Contributor

Choose a reason for hiding this comment

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

we don't want to throw exceptions here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok!

Copy link
Contributor

Choose a reason for hiding this comment

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

The error and the T can be stored in one memory buffer, e.g.

alignas(std::max(alignof(T), sizeof(swift::Error))) char buffer[std::max(sizeof(T), sizeof(swift::Error)];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, I'll try to use it!

Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this to a detail namespace like _impl.

@hyp
Copy link
Contributor

hyp commented Nov 18, 2022

@swift-ci please test

@hyp
Copy link
Contributor

hyp commented Nov 18, 2022

@swift-ci please test

@Robertorosmaninho Robertorosmaninho force-pushed the interop/ExpectedClassForErrorHandling branch from 788edb9 to c409935 Compare November 30, 2022 01:39
@Robertorosmaninho Robertorosmaninho force-pushed the interop/ExpectedClassForErrorHandling branch 2 times, most recently from 97cf443 to 709a75b Compare December 8, 2022 22:35
@Robertorosmaninho Robertorosmaninho requested a review from hyp December 8, 2022 22:58
@hyp
Copy link
Contributor

hyp commented Dec 9, 2022

@swift-ci please test

@hyp
Copy link
Contributor

hyp commented Dec 9, 2022

@swift-ci please test source compatibility

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can extract the return type printing code into a closure in this function, to avoid repeating this code?

@hyp
Copy link
Contributor

hyp commented Dec 13, 2022

@swift-ci please test Windows platform

@hyp
Copy link
Contributor

hyp commented Dec 13, 2022

@swift-ci please test

@Robertorosmaninho Robertorosmaninho force-pushed the interop/ExpectedClassForErrorHandling branch from 2f40d11 to 5fc55b6 Compare December 14, 2022 15:59
@hyp
Copy link
Contributor

hyp commented Dec 14, 2022

@swift-ci please test

@hyp
Copy link
Contributor

hyp commented Dec 15, 2022

@swift-ci please test Windows platform

@hyp
Copy link
Contributor

hyp commented Dec 15, 2022

@swift-ci please test Windows platform

@hyp
Copy link
Contributor

hyp commented Dec 15, 2022

@swift-ci please test Windows platform

…n SwiftToCxxToSwift/hide-swift-module-namespace-in-swift.swift
@hyp
Copy link
Contributor

hyp commented Dec 16, 2022

@swift-ci please test Windows platform

@hyp
Copy link
Contributor

hyp commented Dec 16, 2022

@swift-ci please test

@hyp
Copy link
Contributor

hyp commented Dec 16, 2022

@swift-ci please test source compatibility

@hyp hyp merged commit 2c83ff7 into swiftlang:main Dec 17, 2022
@Robertorosmaninho Robertorosmaninho deleted the interop/ExpectedClassForErrorHandling branch January 3, 2023 22:39
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