Uploaded image for project: 'Mesos'
  1. Mesos
  2. MESOS-2716

Add non-const reference version of Option<T>::get.

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 0.23.0
    • stout

    Description

      Currently Option only provides a const reference to the underlying object:

      template <typename T>
      class Option
      {
        ...
        const T& get() const;
        ...
      };
      

      Since we use Option as a replacement for NULL, we often have optional variables that we need to perform non-const operations on. However, this requires taking a copy:

          static void cleanup(const Response& response)
          {
            if (response.type == Response::PIPE) {
              CHECK_SOME(response.reader);
              http::Pipe::Reader reader = response.reader.get(); // Remove const.
              reader.close();
            }
          }
      

      Taking a copy is hacky, but works for shared objects and some other copyable objects. Since Option represents a mutable variable, it makes sense to add non-const reference access to the underlying value:

      template <typename T>
      class Option
      {
        ...
        const T& get() const;
        T& get();
        ...
      };
      

      Attachments

        Activity

          People

            balamark Mark Wang
            bmahler Benjamin Mahler
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: