fp
Functional Programming extensions to C++ for ROS projects.
|
This tutorial will show you how to write functions that combine Result<T>s. A common reason for this is you are building a struct and the construction of the various operations you do to build that struct could fail. One common place where you might write code like this is when loading ROS parameters.
For this example lets assume we have a function that loads ros parameters with this interface.
And you have a struct that represents your parameters that you'd like to load.
The function we want to write will have this signature:
The first we will do in our function is load our three values.
Next we need to check if any of these failed before we can construct the result. To do this we use the helper function fp::maybe_error
.
In this case the error
variable is a std::optional<fp::Error>
which means that if it contains a value of type fp::Error
it will be true
when cast to bool. The statement inside the if statement is needed to create a Result<Parameters>
type containing the error that we received.
Now that we are past that if statement we know that all our results are valid and we can use them to construct the Parameters
struct.
In this tutorial you learned about a convenience function fp::maybe_error
you can use to check many results before using them.