-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Add structured bindings compatibility #164
Comments
There seems something wrong with this idea. Structured bindings are designed to work with product types, like aggregates, arrays, tuples. Whereas auto r [has_error, val, error] = result<int, Error>{failure(Error::BadInput)}; What value does |
Well, I was just responding to an email there where the bloke pointed out that result is not a sum type, but is really a tuple and ought to quack like one. And he wants structured bindings on it. And he made lots of arguments about how pretty the code looks etc, specifically that typing .value() and .error() all the time is clumsy and litters the screen unnecessarily. In the end, I can't marshal a technical argument against it. A structured bindings interface is safe to use. And I do see how some would find the code looks cleaner. To answer your question, the structured bindings interface would only be available for result and outcome types whose value, error and exception types all have default constructors. So: auto [has_error, val, error] = result<int, Error>{failure(Error::BadInput)}; ... would set |
Maybe inside the implementation it is a tuple. But at the interface level it is a variant. I acknowledge that it is implementable for a subset of instantiations of I am interested to see the examples that illustrate the usefulness. Maybe what would be useful instead is a pattern matching interface: match(func(...),
[](auto&& val) { /* use val */ },
[](auto&& error) { /* use error */ }
); |
Could we make the provision of structured bindings no-value policy determined? So, if the no-value policy is hard UB, that seems much more fine to me to permit structured bindings. If the no-value policy is throw on access of valueless, then structured bindings would not be available by default. This would preserve the spirit of the library I think. Regarding matching, absolutely yes please to that. But I think it should be its own library, made generic and reusable. I also think it's easy to do something nice for a limited use set. I think it's hard to design something elegant which is highly reusable. Or put another way, I don't want to start such a design until at least 2020 given my current work queue. |
How about this?
Kind regards ... |
For some odd reason, some odd people want to write:
... where
func()
returnsresult<T, E>
.Implementing this is trivial: specialise
tuple_size<basic_result<>>
to return 3 and implementat<I>()
. But I suspect given the dragging in of<tuple>
, this support needs its own header file outside the standard set of inclusions.The text was updated successfully, but these errors were encountered: