Uploaded image for project: 'Swift Driver'
  1. Swift Driver
  2. SWIFT-238

Move Document.filter into a separate extension

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • None
    • 0.0.8
    • None

    Description

      The compiler complains as follows:

      /Users/kaitlinmahar/mongo-swift-driver/Sources/MongoSwift/BSON/Document+Sequence.swift:42:17: warning: instance method 'filter' nearly matches defaulted requirement 'filter' of protocol 'Sequence'
          public func filter(_ isIncluded: (KeyValuePair) throws -> Bool) rethrows -> Document {
                      ^
      /Users/kaitlinmahar/mongo-swift-driver/Sources/MongoSwift/BSON/Document+Sequence.swift:42:17: note: candidate has non-matching type '((Document.KeyValuePair) throws -> Bool) throws -> Document' (aka '(((key: String, value: Optional<BSONValue>)) throws -> Bool) throws -> Document') [with Element = Document.KeyValuePair, Iterator = DocumentIterator, SubSequence = Document]
          public func filter(_ isIncluded: (KeyValuePair) throws -> Bool) rethrows -> Document {
                      ^
      /Users/kaitlinmahar/mongo-swift-driver/Sources/MongoSwift/BSON/Document+Sequence.swift:42:17: note: move 'filter' to another extension to silence this warning
          public func filter(_ isIncluded: (KeyValuePair) throws -> Bool) rethrows -> Document {
                      ^
      Swift.Sequence:8:17: note: requirement 'filter' declared here
          public func filter(_ isIncluded: (Self.Element) throws -> Bool) rethrows -> [Self.Element]
                      ^
      

      I had placed this method inside Sequence because I mistakenly thought it was overriding the default implementation of filter Sequence provides. However, it appears that is not the case, and that the user actually still has access to the other impl if they explicitly request a return type of [Document.Element]. The one we provide is simply the default when an array is not requested:

              let doc: Document = ["x": 1, "y": 2]
       
              let arr: [Document.Element] = doc.filter { $0.key != "x" }
              print(arr) // prints [(key: "y", value: Optional(2))]
       
              let doc2 = doc.filter { $0.key != "x" }
              print(doc2) // prints { "y" : 2 }
      

      We can easily fix this warning by just placing the filter method in an extension of its own.

      We should also add a test verifying the behavior of our filter vs the default filter.

      Attachments

        Activity

          People

            kaitlin.mahar@mongodb.com Kaitlin Mahar
            kaitlin.mahar@mongodb.com Kaitlin Mahar
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: