Mike Slinn

Connoisseur of Technology

Scala Type Parameters, Implicit Manifests and Salat

2012-08-02 / All Blog posts

Type parameters provide types for constructing any arguments that are implicit manifests.

This helps when working with Salat. In the following code, dbo is a com.mongodb.DBObject, perhaps retrieved as the result of a find(). Salat uses _typeHint to store the fully qualified name of the persisted class; this property is returned as one of the properties of dbo. The call to Salat's grater() method converts dbo into the desired type. The type is constrained using a lower bound to be a subclass of PubSubAction.

def findWithSeqNum[T <: PubSubAction](seqNum: Long)
    (implicit manifest: Manifest[T]): Option[T] = {
  val klass = manifest.erasure.asInstanceOf[Class[T]]
  val item = myCollection.findOne(
    MongoDBObject("seqNum" -> seqNum, "_typeHint" -> klass.getName))
  item match {
    case Some(dbo: DBObject) =>
      Some(grater(ctx, Manifest.classType(klass)).asObject(dbo))

    case None =>
      None
  }
}

When called as follows, the type parameter need not be provided because there is an an explicit manifest argument:

findWithSeqNum(11)(Manifest.classType(classOf("com.micronautics.Blah"))

When called as follows, the type parameter provides the value for constructing the implicit manifest argument:

findWithSeqNum[Blah](11)

Of course, you can also explicitly define an implicit manifest at any scope that you desire:

implicit val manifest: ClassManifest[Blah]
findWithSeqNum(11)

If the type of the manifest is dynamic (only known at runtime), you can define manifest this way:

val mtype = "com.micronautics.Blah"
implicit val manifest = Manifest.classType(Class.forName(mtype))
findWithSeqNum(11)

Contact Mike Slinn

Unless you are a recruiter, in which case you should not try to make contact!

  • Email
  • Direct: 514-418-0156
  • Mobile: 650-678-2285

Disclaimer

The content on this web site is provided for general information purposes only and does not constitute legal or other professional advice or an opinion of any kind. Users of this web site are advised to seek specific legal advice by contacting their own legal counsel regarding any specific legal issues. Michael Slinn does not warrant or guarantee the quality, accuracy or completeness of any information on this web site. The articles published on this web site are current as of their original date of publication, but should not be relied upon as accurate, timely or fit for any particular purpose.

Accessing or using this web site does not create a client relationship. Although your use of the web site may facilitate access to or communications with Michael Slinn via e-mail or otherwise via the web site, receipt of any such communications or transmissions does not create a client relationship. Michael Slinn does not guarantee the security or confidentiality of any communications made by e-mail or otherwise through this web site.

This web site may contain links to third party web sites. Monitoring the vast information disseminated and accessible through those links is beyond Michael Slinn's resources and he does not attempt to do so. Links are provided for convenience only and Michael Slinn does not endorse the information contained in linked web sites nor guarantee its accuracy, timeliness or fitness for a particular purpose.


comments powered by Disqus

© 1976-2020, Michael Slinn. All rights reserved.