1

I'm trying to build following

I have a parent generic class

abstract class ResultProvider[+T: Writes](db: DB) {
  def get(id: Long): Future[Seq[T]]
}

And some implementations, e.g.

class LengthProvider(db: DB) extends ResultProvider[LengthResult](db){
  override def get (userId: Long): Future[Seq[LengthResult]] = ...
}

object LengthProvider extends ((DB) => DisciplinePredictor) {
  override def apply(db: DB) = new LengthProvider(db)
}

I have following configuration map:

val providers: Map[String, ((DB) => ResultProvider[???])] = Map(
  "length" -> LengthProvider,
  "width"  -> WidthProvider,
   ...
)

My question is what should I put in place of ???. Ideally, it should be something like T : Writes, as I only care that this type has Writes implicit implemented, as I'm going to Json.toJson it. It will compile with Any, but then the information that the class should implement Writes implicit is lost. Or should I use a different approach? I could probably create a superclass for all my result case classes (e.g.LengthResult), but I want to get away with the implicits.

1
  • @AlexanderArendar That's not what is wanted at all. Commented Apr 12, 2016 at 14:02

1 Answer 1

1

You should be able to write ResultProvider[_] (search for "existential types" if you are unfamiliar with this syntax), but you'll need to give a name to the implicit:

abstract class ResultProvider[+T](db: DB)(implicit val writes: Writes[T]) { ... }

Elsewhere:

val provider: ResultProvider[_] = providers("length")
import provider.writes // makes the implicit visible here
...

You might need to help the compiler (or yourself, if you need to name the type) by providing a type variable:

providers("length") match {
  case provider: ResultProvider[a] =>
    import provider.writes 
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

It worked! Can you please elaborate why I need to help the compiler with a type variable (i.e. a)? It doesn't work otherwise.
Unfortunately, this depends on details of compiler's type inference which I don't understand. I've just been in similar situations before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.