From 1c4515fb16ecd290c295ea20d59516ea02e6c149 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Fri, 19 Mar 2021 14:25:23 -0700 Subject: [PATCH] Document alternative for declaring an optional parameter While we could conditionally declare a parameter by using a second parameter, we can also just catch the expected exception in the case that no parameter override is provided. I think this makes for a better user experience. Signed-off-by: Jacob Perron --- rclcpp/doc/notes_on_statically_typed_parameters.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rclcpp/doc/notes_on_statically_typed_parameters.md b/rclcpp/doc/notes_on_statically_typed_parameters.md index b96866c639..b772348e8d 100644 --- a/rclcpp/doc/notes_on_statically_typed_parameters.md +++ b/rclcpp/doc/notes_on_statically_typed_parameters.md @@ -85,6 +85,16 @@ if (mode == "modeB") { } ``` +Or, wrap the declaration in a try-catch: + +```cpp +try { + node->declare_parameter("param_that_is_optional"); +} catch (const rclcpp::exceptions::NoParameterOverrideProvided &) { + // a parameter value was not provided +} +``` + ## Other migration notes Declaring a parameter with only a name is deprecated: