Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all visible implicits

If it is at all possible, how to list all visible implicits using both reflection and macros? I need this to be able to analyze available typeclass instances.

A use-case

case class Artist (name: String, genres: Set[Genre])
case class Genre (name: String)

object CatalogueDB extends MySQL {
  implicit val artistEntity = new Entity[Artist] {...}
  implicit val genreEntity = new Entity[Genre] {...}
}

trait MySQL {
  // Typeclasses let me restrict methods on `MySQL` to work only for types, 
  // which are associated with its instance.
  // E.g., the following method will only compile when either an `Artist` or
  // a `Genre` is passed to it when called on `CatalogueDB`:
  def save[e : Entity](e : e) ...
  // But also I need to be able to get a list of those types and 
  // instances of `Entity` typeclass to get the information
  // needed to generate the database schema and 
  // to perform other tasks like that.
  // So, the following method should somehow be able to have
  // the `Entity` instances for `Artist` and `Genre` at its disposal,
  // when called on `CatalogueDB`.
  def generateSchema ...
}
like image 724
Nikita Volkov Avatar asked Nov 11 '22 16:11

Nikita Volkov


1 Answers

I don't think there's a way of doing exactly that at the moment. However, listing all members of a given class and checking whether they are implicit or not should be relatively easy - both at compile-time and at runtime.

like image 159
Eugene Burmako Avatar answered Nov 15 '22 11:11

Eugene Burmako