获取Scala中的泛型类型的运行时类型

时间:2017-04-27 09:55:31

标签: scala reflection

我有以下特点:

      trait CSVRowParser[A] {
        def parse(row: Seq[String]): Try[A]
      }

      object CSVRowParser {
        implicit def all[A, H <: HList](implicit gen: Generic.Aux[A, H],
          read: CSVRowReader[H]): CSVRowParser[A] = new CSVRowParser[A] {
          def parse(row: Seq[String]): Try[A] = {
            read.from(row).map(gen.from)
          }
        }
        def apply[A](implicit ev: CSVRowParser[A]): CSVRowParser[A] = ev
  }

我有另一个名为CSVReader的课程:

class CSVReader[A: CSVRowParser] {

   def parse(path: String): ReaderWithFile[A] = ReaderWithFile[A](path)

   case class ReaderWithFile[B: CSVRowParser](path: String) {
    ...
    // how can I here identify the type of B?
   }

}
然后我按照这样的方式打电话:

  def apply[A: CSVRowParser] = new CSVReader[A]

  val reader = apply[Address]
  val withCustomConfig: Seq[Address] = reader parse "/csv-parser/address.csv" using CSVParserConfig(Pipe)

如何在ReaderWithFile案例类中获取A的运行时类型?

0 个答案:

没有答案
相关问题