问题1 - 基本LUBConstraints
我第一次尝试使用现有的LUBConstraints失败,因为缺少证据(参见下面的代码块).任何暗示为什么?空列表不是有效的长列表吗?没有元素违反约束.
import shapeless.ops.coproduct
import shapeless.{::, :+:, Coproduct, HNil, HList}
object testLUBConstraints {
import shapeless.LUBConstraint._
// !!! see comment on question - this satisfies the implicit below!!!
// implicit val hnilLUBForLong = new LUBConstraint[HNil.type, Long] {}
def acceptLong[L <: HList : <<:[Long]#?](l: L) = true
val validLong = acceptLong(1l :: HNil)
val validEmpty = acceptLong(HNil)
// => WHY??? Error: could not find implicit value for evidence parameter of type shapeless.LUBConstraint[shapeless.HNil.type,Long]
// MY EXPECTATION WAS: 'implicit def hnilLUB[T] = …Run Code Online (Sandbox Code Playgroud) 我使用那种线来测试我的暗示,使其隐含于复制粘贴事故.我花了很长时间才弄清楚,为什么这个编译尽管我不期望它编译:
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66).
... skipped some comment lines ...
scala> case object Foo
defined object Foo
scala> object Bar { implicit val f: Foo.type = implicitly[Foo.type] }
defined object Bar
scala> val x = Bar.f
x: Foo.type = null
scala>
Run Code Online (Sandbox Code Playgroud)
我希望Bar编译失败,因为没有类型的隐式val Foo.type(case对象未声明为隐式).
对我来说,看起来编译器使用f自己的声明(左侧)来完成其实现(右侧).
这真的是预期的行为吗?在运行时,这会导致null值出现意外行为(对我来说主要是NPE).