Weird currying of Update function of MongoCollection class of Casbah Mongo driver

104 views Asked by At

As you know casbah mongodb driver has an update function like this :

def update [A, B] (q: A, o: B)(implicit arg0: (A) ⇒ DBObject, arg1: (B) ⇒ DBObject) : WriteResult

I think i understand currying concept of scala. However, As far as I know this update function is supposed to be used like this :

collection.update(MongoDBObject(...), MongoDBObject(...))

This confuses me. As I do not fill second argument list of the update method, I would think above expression would return a function like :

(implicit arg0: (A) ⇒ DBObject, arg1: (B) ⇒ DBObject) => WriteResult

However it does not. Is it because of the implicit definiton for the arguments in the second function argument list ?

1

There are 1 answers

0
Ross On BEST ANSWER

The Casbah library subscribes to the "pimp my library" pattern. It extends the official MongoDB Java driver and adds implicits and other functionality to make using the Java driver feel "native" in scala.

The update method looks like this:

  /**
   * Performs an update operation.
   * @param q search query for old object to update
   * @param o object with which to update <tt>q</tt>
   */
  def update[A, B](q: A, o: B, upsert: Boolean = false, multi: Boolean = false,concern: com.mongodb.WriteConcern = this.writeConcern
this.writeConcern)(implicit queryView: A => DBObject,
                            objView: B => DBObject,
                            encoder: DBEncoder = customEncoderFactory.map(_.create).orNull): WriteResult = {
    underlying.update(queryView(q), objView(o), upsert, multi, concern, encoder)
}

The Java driver requires DBObject for its update operation, but Casbah provides a Scala helper for creating DBObjects in MongoDBObject. To provide interop between the Scala types and the Java type we can make use of implicit parameters.

As the compiler will know A at compile time, it can determine whether an implicit definition of type A => DBObject is in scope. Casbah then uses that method queryView(q) which converts q to DBObject and passes it to the underlying update.