2016-07-31 49 views
0

鉴于发散Implicits从以下特拉维斯布朗的教育和良好的书面Type classes and generic derivation的案例类瓦特/箱类参数

case class Person(name: String, age: Double) 

trait Parser[A] { 
    def apply(s: String): Option[A] 
    } 

    implicit val hnilParser: Parser[HNil] = new Parser[HNil] { 
    def apply(s: String): Option[HNil] = if(s.isEmpty) Some(HNil) else None 
    } 

    implicit def hconsParser[H: Parser, T <: HList: Parser]: Parser[H :: T] = new Parser[H :: T] { 
    def apply(s: String): Option[H :: T] = s.split(",").toList match { 
     case cell +: rest => for { 
     head <- implicitly[Parser[H]].apply(cell) 
     tail <- implicitly[Parser[T]].apply(rest.mkString(",")) 
     } yield head :: tail 
    } 
    } 

    implicit val stringParser: Parser[String] = new Parser[String] { 
    def apply(s: String): Option[String] = Some(s) 
    } 

    implicit val intParser: Parser[Int] = new Parser[Int] { 
    def apply(s: String): Option[Int] = Try(s.toInt).toOption 
    } 

    implicit val doubleParser: Parser[Double] = new Parser[Double] { 
    def apply(s: String): Option[Double] = Try(s.toDouble).toOption 
    } 

    implicit val booleanParser: Parser[Boolean] = new Parser[Boolean] { 
    def apply(s: String): Option[Boolean] = Try(s.toBoolean).toOption 
    } 

    implicit def caseClassParser[A, R <: HList](implicit gen: Generic[A] { type Repr = R }, 
               reprParser: Parser[R]): Parser[A] = 
    new Parser[A] { 
     def apply(s: String): Option[A] = reprParser.apply(s).map(gen.from) 
    } 


    object Parser { 
    def apply[A](s: String)(implicit parser: Parser[A]): Option[A] = parser(s) 
    } 

    implicit val stringParser: Parser[String] = new Parser[String] { 
    def apply(s: String): Option[String] = Some(s) 
    } 

    implicit val intParser: Parser[Int] = new Parser[Int] { 
    def apply(s: String): Option[Int] = Try(s.toInt).toOption 
    } 

    implicit val doubleParser: Parser[Double] = new Parser[Double] { 
    def apply(s: String): Option[Double] = Try(s.toDouble).toOption 
    } 

我很好奇,试图得到一个Parser[X]其中X是一个案例类与Person的说法,即一个案例类:

case class PersonWrapper(person: Person, x: Int) 

然而,我得到一个错误:

scala> Parser[PersonWrapper]("kevin,66,42") 
<console>:15: error: diverging implicit expansion for type net.Test.Parser[net.Test.PersonWrapper] 
starting with method caseClassParser in object Test 
     Parser[PersonWrapper]("kevin,66,42") 
          ^

首先,为什么会出现这种发散的隐式错误?

其次,是否可以使用上面的代码来获得Parser[PersonWrapper]

+0

你确定你没有隐含在REPL范围内吗?我没有得到分歧的隐含错误。 –

+0

嗯。那么,你用Parser [PersonWrapper](“kevin,66,42”)得到了什么结果呢?以下是我的全部结果:https://gist.github.com/kevinmeredith/f4995cdf3bc61907bef76182bee69b39 –

回答

1

Secondly, is it possible to use the above code to get a Parser[PersonWrapper]?

没有,只是跳到本文的结尾:

scala> case class BookBook(b1: Book, b2: Book) 
defined class BookBook 

scala> Parser[BookBook]("Hamlet,Shakespeare") 
    res7: Option[BookBook] = None 

Our format doesn’t support any kind of nesting (at least we haven’t said anything about nesting, and it wouldn’t be trivial), so we don’t actually know how to parse a string into a BookBook...

的问题是,在cellcase cell +: rest永远只能是没有传递到implicitly[Parser[H]].apply(cell)逗号的字符串。对于PersonWrapper,这意味着第一个单元格将试图做到这一点:

implicitly[Parser[PersonWrapper]].apply("kevin") 

这显然无法解析。为了使嵌套的解析器正常工作,在将Parser[H]应用于它们之前,需要一些方法将这些单元分组在一起。