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

ItoA type should also have a named StringData getter

    XMLWordPrintableJSON

Details

    • Service Arch
    • Fully Compatible
    • Service Arch 2023-08-07

    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

            billy.donahue@mongodb.com Billy Donahue
            mathias@mongodb.com Mathias Stearn
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: