This page is no longer maintained — Please continue to the home page at

Problem with primitive type implicit conversion

No replies
Joined: 2009-09-21,
User offline. Last seen 3 years 3 weeks ago.
I'm making a DB-type toolkit and I'd like to convert between objects and primary keys, implicitly.  My code works if I define primary keys as wrapped longs, but it won't compile if I define them as a parameterized type of long.
If I don't use a manifest on the implicit conversion, I get this error, "value name is not a member of Nothing," but if I do use a manifest, I get this error, "value name is not a member of pktest.PK_DEFS.PK[pktest.Person]".  Here's my example code, with the PK[T] type uncommented and the PK[T] class commented out.

Can anyone help me with this?

-- Bill

object PK_DEFS { type PK[T] = Long val NULL_PK = -1 def pkFor[T](pk: Long) = pk.asInstanceOf[T]// class PK[T](val pk: Long) {override def toString() = "ID("+pk+")"} // val NULL_PK = null// def pkFor[T](pk: Long) = new PK[T](pk)
trait HasPK[T] {def pk: PK[T]}
implicit def pk2Obj[T: Manifest](pk: PK[T]): T = storage(pk).asInstanceOf[T] implicit def obj2Pk[T](obj: HasPK[T]) =
var storage = Map[PK[_], Any]()
def store(obj: HasPK[_]) = storage += -> obj def get[T](pk: PK[T]) = storage(pk) }
import PK_DEFS._
class Person(val pk: PK[Person], var name: String, var friend: PK[Person]) extends HasPK[Person] {  override def toString = "Person("+name+")" }
object PkTest {  import PK_DEFS._
  def main(args: Array[String]) {  val p1 = new Person(pkFor(1), "joe", NULL_PK)  store(p1)  val p2 = new Person(pkFor(2), "fred", p1)  store(p2)  println(  println(p2.friend)  println(   }}

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland