Scala Shapeless:试图通过类型限制HList元素

Scala Shapeless:试图通过类型限制HList元素,scala,constraints,shapeless,hlist,Scala,Constraints,Shapeless,Hlist,问题1-基本约束 我第一次尝试使用现有约束失败,因为缺少证据(请参阅下面的代码块)。有什么提示吗?空列表不是有效的长列表吗?没有元素违反约束 import shapeless.ops.coproduct import shapeless.{::, :+:, Coproduct, HNil, HList} object testLUBConstraints { import shapeless.LUBConstraint._ // !!! see comment on question

问题1-基本约束

我第一次尝试使用现有约束失败,因为缺少证据(请参阅下面的代码块)。有什么提示吗?空列表不是有效的长列表吗?没有元素违反约束

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] = new LUBConstraint[HNil, T] {}' defined within LUBConstraint companion should provide so

  // val invalid = acceptLong(1.0d :: HNil) -> fails due to missing evidence (as expected)
}
导入shapeless.ops.coproduct
导入无形状。{:,:+:,余积,HNil,HList}
对象测试约束{
导入无形状约束_
//!!!请参见对问题的评论-这满足了下面的隐含要求!!!
//隐式val hnilLUBForLong=new LUBConstraint[HNil.type,Long]{}

def acceptLong[L值
HNil
的推断类型将是单例类型
HNil.type
,它是
HNil
的一个适当子类型。因为像
LUBConstraint
这样的类型类是不变的,如果您请求
HNil.type
的实例,将找不到
HNil
的任何可用实例


有人试图改变HNil的定义,这样才能起作用,但这不是小事,也不清楚改变的所有含义是否都是可取的。与此同时,你可以编写
HNil:HNil
来向上转换值。

我设法绕过了第一个错误(在问题1的代码块中)通过局部声明
implicit val hnilLUBForLong=new-LUBConstraint[HNil.type,Long]{}
,似乎声明的方法提供的隐式值与类型不完全匹配:
LUBConstraint[HNil,Long]=!=LUBConstraint[HNil.type,Long]
意图还是一个bug?正在研究CPConstraint的隐式解析(参见问题2)拥有
val hlLong::::[Long,HNil]=1L::HNil
并使用
implicit val cpcLong=implicit[CPConstraint[hlLong.type,CPType]]
我尝试了
implicit val scptylong1:CPConstraint[:[Long,HNil],CPType]=新的CPConstraint[:[Long,HNil],CPType]{}
,它不适合隐式搜索,但
隐式val scpcEmptyLong2:CPConstraint[hlLong.type,CPType]=新的CPConstraint[hlLong.type,CPType]{}
会。-为什么???有什么区别?这可能是三个独立的问题。在第一种情况下,您可以通过显式键入
HNil
作为
HNil
acceptLong(HNil:HNil)
应该编译来解决问题。