Skip to content

Instantly share code, notes, and snippets.

@bwmcadams
Created June 9, 2011 15:20
Show Gist options
  • Select an option

  • Save bwmcadams/1016955 to your computer and use it in GitHub Desktop.

Select an option

Save bwmcadams/1016955 to your computer and use it in GitHub Desktop.
Defaults + Type Classes
/**
* Attempting to factor this method (Which used to take a hard argument for Qry of BSONDocument
* to use a type class, where the type class object SerializableBSONObject knows how to encode/decode the
* Document represented in Qry
*
* I was ***hoping*** That the Scala compiler would be smart enough to extract the type class type
* in the case of a Default Argument but it seems not.
* The compiletime error is:
*
* could not find implicit value for evidence parameter of type org.bson.SerializableBSONObject[Nothing]
*
* A cookie and/or mug of tasty beer for anyone who has clever ideas that don't involve
* java style overloaded methods
*/
def findAndRemove[Qry : SerializableBSONObject](db: String)(collection: String)(query: Qry = Document.empty) = findAndModify(db)(collection)(query=query, remove=true)_
@eed3si9n
Copy link
Copy Markdown

eed3si9n commented Jun 9, 2011

I am not seeing the org.bson.SerializableBSONObject[Nothing].

trait Qry {}
class Document extends Qry {
}
object Document {
  def empty = new Document
}
trait SerializableBSONObject[A] {}
object SerializableBSONObject {
  implicit val serialize: SerializableBSONObject[Document] = new SerializableBSONObject[Document] {}
}
object Foo {
  def findAndRemove[Qry: SerializableBSONObject](db: String)(query: Qry = Document.empty) = "something"
}
object Main {
  def main(args: Array[String]) {
    println (Foo.findAndRemove("")() )
  }
}

@jsuereth
Copy link
Copy Markdown

jsuereth commented Jun 9, 2011

My guess is it's the usage of findAndModify that's burning you here.

@bwmcadams
Copy link
Copy Markdown
Author

bwmcadams commented Jun 9, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment