加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 19:16:39 所属栏目:安全 来源:网络整理
导读:问题1 – 基本LUBConstraints 我第一次尝试使用现有的LUBConstraints失败,因为缺少证据(参见下面的代码块).任何暗示为什么?空列表不是有效的长列表吗?没有元素违反约束. import shapeless.ops.coproductimport shapeless.{::,:+:,Coproduct,HNil,HList}obj
问题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] = 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 – 使用Coproduct自己的约束
(分成一个单独的问题:Shapeless: own HList constraint using Coproduct)

问题3 – 按参数类型限制案例类
(分成一个单独的问题:Shapeless: restricting case class types)

解决方法

值HNil的推断类型将是单例类型HNil.type,它是HNil的适当子类型.因为像LUBConstraint这样的类型是不变的,所以如果你要求HNil.type的实例,将找不到任何可用的HNil实例.

已经有some discussion改变了HNil的定义,这样就可以了,但这并不是微不足道的,并且不清楚改变的所有含义是否可取.在此期间,您可以编写HNil:HNil来更新该值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读