Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-55618

ItoA type should also have a named StringData getter

    XMLWordPrintable

    Details

      Description

      Only having a conversion operator makes it awkward to work with the string data in the same scope where you declare the ItoA object and makes it tempting to do the wrong thing:

      auto str = ItoA(someNum);
      invariant(str.size() < 4); // doesn't compile
      invariant(str.operator StringData().size()); // compiles, but gross
      invariant(str.sd().size() < 4); // nice, but doesn't compile... yet :)
       
      // danger!
      // We can't prevent this without also preventing the totally valid f(ItoA(someNum)),
      // but we can make it less tempting.
      auto str2 = StringData(ItoA(someNum)); 
      

      As an alternative, we could make ItoA be a subclass of StringData so that it just directly presents the string-like API. It is a bit odd, since that would allow assigning through a mutable StringData& (but not through an ItoA& or an ItoA variable), but since that seems to be harmless, maybe its fine?

        Attachments

          Activity

            People

            Assignee:
            backlog-server-servicearch Backlog - Service Architecture
            Reporter:
            redbeard0531 Mathias Stearn
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated: