类型无形的隐式扩展分散。可变形[...]

时间:2015-10-15 12:53:28

标签: scala implicit shapeless

我的值为 $("button[role='button']:contains('Close')").click(function () { $('.ui-dialog').filter(function () { return $(this).css("display") === "block"; }).find('.ui-dialog-content').dialog('close'); }); 。如果它的类是Any我必须能够将它转换为案例类,为此我使用Travis Brown here提出的解决方案。问题是当我将Map[String, Any]实现定义如下时,在某些情况下,我得到分歧隐式扩展错误:

Typeable

REPL会议:

  implicit def caseClassTypeable[T, R <: HList](implicit castT: Typeable[T],
                                            gen: LabelledGeneric.Aux[T, R],
                                            fromMap: FromMap[R]) = 
new Typeable[T] {
  override def cast(t: Any): Option[T] = t.cast[Map[String, Any]] flatMap (m => to[T].from(m))

  override def describe: String = castT.describe
}

然而这个有效:

scala> import shapeless.syntax.typeable._
import shapeless.syntax.typeable._

scala> case class Simple(foo: String, bar: Int)
defined class Simple

scala> val m: Any = Map("foo" -> "a", "bar" -> 42)
m: Any = Map(foo -> a, bar -> 42)

scala> m.cast[Simple]
res7: Option[Simple] = Some(Simple(a,42))

scala> val l: Any = List(1, 2, 3)
l: Any = List(1, 2, 3)

scala> l.cast[List[Any]]
<console>:22: error: diverging implicit expansion for type shapeless.Typeable[List[Any]]
starting with method inrTypeable in object Typeable
       l.cast[List[Any]]
             ^

有什么想法吗?

0 个答案:

没有答案