You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our parse function produces a 'ada::result' which is an alias for an std::expected<ada::url,...> (from C++23).
Our main objective is to make it very hard for users to access the result without first checking for an error condition. It is a common problem that people make assumptions (the URL is always valid) only to find the assumption violated in production. We want to help users do the right thing naturally.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Our parse function produces a 'ada::result' which is an alias for an
std::expected<ada::url,...>
(from C++23).Our main objective is to make it very hard for users to access the result without first checking for an error condition. It is a common problem that people make assumptions (the URL is always valid) only to find the assumption violated in production. We want to help users do the right thing naturally.
You can use it as like so...
If you forget to check...
It is unsafe code.
If you need to access directly the
ada::url
instance, you can do this...However, creating a copy is wasteful.
You may grab a reference which is efficient:
Returning from functions:
If you have a function returning an ada::url, then you won't copy...
However, it will throw on error.
You can do it this way...
But you will be creating a second struct (default ada::url) so it is not free.
You might as well pass around
ada::result
instances unless you can tell that, for sure, that the url is valid.Performance tip: Avoid copying by moving the result. After checking the value is valid, you can move it to a variable. It is efficient.
Pointers:
If you just need a pointer to the ada::url value contained in the ada::result, you can do this:
It should not entail any copying and should be very efficient. Note that the pointer is only valid as long as
ada::result url
is alive.A pointer is likely even faster than a move!!!
Beta Was this translation helpful? Give feedback.
All reactions