Skip to content

Feature Request: Optional Attributes #850

Open

Description

@Ryanf55

DDS supports the optional attribute in IDL 4.2 specification attribute section 8.3.1.3. In C++, now that we have C++17, we can use std::optional.

A prior dicussion is here: https://discourse.ros.org/t/optional-fields-in-message/991/16

Consider adding a @optional specifier to rosidl that translates to the DDS optional .

Amend the ROS 2 design documentation for Standardized Annotations to add optional:
https://design.ros2.org/articles/idl_interface_definition.html

It's supported by:

Workarounds: Bounded array with a max length of 1, but the syntax is funky.

Activity

  1. alsora commented on Mar 6, 2025

    @alsora

    Hi, we discussed this during the ROS waffle meeting and while we agree that this would be a useful feature, we also recognize that it will require a relatively large amount of work.
    It would be great to get some support from the community.

  2. pinned this issue on Mar 6, 2025
  3. Ryanf55 commented on Mar 7, 2025

    @Ryanf55
    Author

    Thanks. If I end up getting time allocated at work, I'll request being assigned, but otherwise anyone is welcome.

    Would you accept a merge with opt-in support of @optional if I got it working only with the following:

    • FastDDS
    • rosidl_generator_cpp

    We want this feature to integrate with a non-ROS-based software. The only incompatibility is the use of optional.
    Getting this working on all RMW's and all languages is unlikely to be approved, but I could get a subset of the work approved.

  4. ros-discourse commented on Mar 25, 2025

    @ros-discourse

    This issue has been mentioned on ROS Discourse. There might be relevant details there:

    https://discourse.ros.org/t/challenges-with-ros2-on-commercial-robots/42784/8

  5. InvincibleRMC commented on Jul 29, 2025

    @InvincibleRMC
    Contributor

    Hey, I'm interested in taking a stab at adding optional support at least in rosidl_generator_cpp. One design question I have is should I add another class for Optional into rosidl_parser.defintion or simply check whether the member has the optional annotation and update places that use msg_type_to_cpp to something like member_to_cpp. Another is would the default for std::optional<int> be 0 or std::nullopt.

  6. Ryanf55 commented on Jul 29, 2025

    @Ryanf55
    Author

    Hey, I'm interested in taking a stab at adding optional support at least in rosidl_generator_cpp. One design question I have is should I add another class for Optional into rosidl_parser.defintion or simply check whether the member has the optional annotation and update places that use msg_type_to_cpp to something like member_to_cpp. Another is would the default for std::optional<int> be 0 or std::nullopt.

    Nice!

    According to RTI, by default, optional members should be unset, so not default initialized. So, std::nullopt.

    https://community.rti.com/static/documentation/connext-dds/current/doc/manuals/connext_dds_professional/extensible_types_guide/extensible_types/Optional_Members.htm

    By default optional members are unset (dds::core::optional<T>::has_value() is false).

  7. InvincibleRMC commented on Aug 1, 2025

    @InvincibleRMC
    Contributor

    When updating the actual .msg, .srv, or .action files to support optional fields, would you prefer introducing a new optional keyword before the type, using a question mark after the type (e.g., char? for an optional char), or some other approach?

  8. christophebedard commented on Aug 1, 2025

    @christophebedard
    Member

    I think we should do @optional in ROSIDL (.msg/.srv/.action) like OMG IDL does. OMG IDL has other @annotations (e.g., @key, @default, @verbatim) which ROSIDL could potentially also support, so @optional would be more IDL-y.

  9. Ryanf55 commented on Aug 1, 2025

    @Ryanf55
    Author

    I think we should do @optional in ROSIDL (.msg/.srv/.action) like OMG IDL does. OMG IDL has other @annotations (e.g., @key, @default, @verbatim) which ROSIDL could potentially also support, so @optional would be more IDL-y.

    Agree

  10. InvincibleRMC commented on Aug 1, 2025

    @InvincibleRMC
    Contributor

    @christophebedard is there some generic rmw agnostic test set I can use for testing? Trying to determine if my serialization code actually works or is a lie.

  11. christophebedard commented on Aug 1, 2025

    @christophebedard
    Member

    Something like the tests from the packages in this repo? https://github.com/ros2/system_tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions