A) exte...">

Convert object string to "A"

I would like to write a class that looks 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)
}

      

I have several problems:

  • Using implicit with strings doesn't cause any problems
  • In any case, the system does not find the implicit ones, if they are defined in my module, they will need to be defined in the module that creates the class
  • the second method of the apply

    object Store

    just won't compile as it A

    will be removed, so the compiler won't be able to find the conversion from String

    toA

How would you create an object like this that converts a string to some other type? I would not want the user of the library to enter the type rwice (i.e. specifying both the type and the conversion function).

+2


source to share


1 answer


I don't understand what you are trying with the second one apply

. To me it looks like the first one apply

should have an implicit keyword and everything will be done with it. You can either pass this parameter explicitly or leave it if implicit is present. Also, you don't need to pass it explicitly c

, since you should already have it implicitly in the scope of the first one apply

.



I would venture that the second apply

does not compile because the field object Store

is not implicit String => A

.

+1


source







All Articles