7

Question 1 - Basic LUBConstraints

My first try playing around with existing LUBConstraints fails for missing evidence (see code block below). Any hint why? Isn't an empty list a valid list of longs? no element violates the constraint.

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

Any help appreciated.

Question 2 - Own Constraint using Coproduct (split into a seperate question: Shapeless: own HList constraint using Coproduct)

Question 3 - restrict case classes by parameter types (split into a separat question: Shapeless: restricting case class types)

4

1 回答 1

6

该值的推断类型HNil将是单例类型HNil.type,它是 的正确子类型HNil。因为像这样的类型类LUBConstraint是不变的,所以HNil如果您请求HNil.type.

已经有一些关于更改定义的讨论,HNil以便这将起作用,但这并不是微不足道的,并且不清楚更改的所有含义是否都是可取的。同时,您可以写入HNil: HNil以向上转换该值。

于 2015-09-25T22:17:52.993 回答