jeudi 14 avril 2016

Check presence of handler for each type in class on compile-time

I have a huge plain old class I have not control over:

class Legacy {
  @BeanProperty var app_id: String = _
  @BeanProperty var platform: Int = _
  // a lot of fields
}

Also I have code which should handle each field properly (user decides in runtime which field should be processed) depending on it's type, so I have a Map[String, Handler] which contain subclasses of Handler for each particular type:

def getHandler(fieldType: String): Handler = {
  Map(
    "String" -> StringHandler,
    "scala.Int" -> IntHandler
  ).getOrElse(fieldType, AnyRefHandler)
}

What I want to have is compile-time assurance that all fields in Legacy class have appropriate handler object and if author of Legacy added new field to it with new type my library should not compile. Or even better a compile-time warning to add a handler.

I think I can use implicit evidence here, but don't know how its resolution should look like. Any ideas?





Aucun commentaire:

Enregistrer un commentaire