views:

84

answers:

1

I would like to write a class looking like this:

class Store[+A](dest: Symbol)(implicit c: String => A) extends Action(dest) {
  override def update(options: HashMap[Symbol,Any], arg: String): Unit = {
    options += ((dest -> c(arg)))
  }
}

object Store {
  def apply[A](dest: Symbol)(c: String=>A) = new Store[A](dest)(c)
  def apply[A](dest: Symbol) = new Store[A](dest)
}

Doing so, I have a few problems:

  1. Using implicit with strings causes no end of trouble
  2. Anyway, the system doesn't find the implicit if they are defined in my module, they would need to be defined in the module creating the class
  3. the second apply method of the Store object just doesn't compile as A will be erased so the compiler has no way of finding a conversion from String to A

How would you create such an object that convert a string to some other type? I wouldn't want the user of the library to enter the type rwice (i.e. by specifying both the type and the conversion function).

+1  A: 

I don't understand what you are trying with the second apply. To me, it looks like the first apply should have the implicit keyword, and you'd be done with it. You can either pass the parameter explicitly, or leave it out if an implicit is present. Also, you wouldn't need to pass c explicitly, since you'd already have it implicitly in the scope of the first apply.

I'd venture the second apply doesn't compile because there's no implicit String => A available in the scope of object Store.

Daniel