小编sth*_*elo的帖子

无形:尝试按类型限制HList元素

问题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)

scala constraints hlist shapeless

7
推荐指数
1
解决办法
786
查看次数

scala隐含完成自己?

我使用那种线来测试我的暗示,使其隐含于复制粘贴事故.我花了很长时间才弄清楚,为什么这个编译尽管我不期望它编译:

> 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).

null scala implicit

5
推荐指数
1
解决办法
101
查看次数

标签 统计

scala ×2

constraints ×1

hlist ×1

implicit ×1

null ×1

shapeless ×1