Reputation: 43264
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:
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).
Upvotes: 2
Views: 214
Reputation: 297205
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
.
Upvotes: 1