6
問題1 - 基本LUBConstraints無形:試圖通過它們的類型來限制HList元素
我第一次嘗試與現有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] = new LUBConstraint[HNil, T] {}' defined within LUBConstraint companion should provide so
// val invalid = acceptLong(1.0d :: HNil) -> fails due to missing evidence (as expected)
}
任何幫助表示讚賞。
問題2 - 使用餘積 烏龍約束(分裂成一個單獨的問題:Shapeless: own HList constraint using Coproduct)
問題3 - 由參數類型限制的情況下的類 (分成分隔符問題:Shapeless: restricting case class types)
我設法通過局部聲明隱式的val hnilLUBForLong = new LUBConstraint [HNil.type,Long] {}'來解決第一個ERROR(在_Question 1_的代碼塊中) - 似乎由聲明提供的隱式值方法沒有完全匹配類型:'LUBConstraint [HNil,Long] =!= LUBConstraint [HNil.type,Long]' 意圖甚至是一個錯誤? – sthielo
處理CPConstraint的隱式解析(參見_Question 2_) 具有'val hlLong::: [Long,HNil] = 1L :: HNil'並隱含地用隱式val查找它的CPConstraint cpcLong =隱式[CPConstraint [ hlLong.type,CPType]]''我試過'隱式val scpcEmptyLong1:CPConstraint [:: [Long,HNil],CPType] = new CPConstraint [:: [Long,HNil],CPType] {} **適合隱式搜索但隱含的val scpcEmptyLong2:CPConstraint [hlLong.type,CPType] =新的CPConstraint [hlLong.type,CPType] {}'會。 - 爲什麼?有什麼不同? – sthielo
這應該是三個不同的問題。在第一種情況下,您可以通過將'HNil'明確輸入爲'HNil'來解決問題:'acceptLong(HNil:HNil)'應該編譯。 –