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

scala – 为什么无形的_0 Nat是一个类而不是一个对象?

发布时间:2020-12-16 19:14:17 所属栏目:安全 来源:网络整理
导读:我想了解Shapeless,我遇到了这个: // Base trait for type level natural numbers.trait Nat { type N : Nat}// Encoding of successor.case class Succ[P : Nat]() extends Nat { type N = Succ[P]}// Encoding of zero.class _0 extends Nat { type N = _
我想了解Shapeless,我遇到了这个:

// Base trait for type level natural numbers.
trait Nat {
  type N <: Nat
}

// Encoding of successor.
case class Succ[P <: Nat]() extends Nat {
  type N = Succ[P]
}

// Encoding of zero.
class _0 extends Nat {
  type N = _0
}

_0是一个特殊而独特的案例,如列表的Nil. _0没有前任.为什么它不是一个对象/案例对象(单例)? HLists似乎这样做:

// `HList` ADT base trait.
sealed trait HList

// Non-empty `HList` element type.
final case class ::[+H,+T <: HList](head : H,tail : T) extends HList {
  override def toString = head+" :: "+tail.toString
}

// Empty `HList` element type.
sealed trait HNil extends HList {
  def ::[H](h : H) = shapeless.::(h,this)
  override def toString = "HNil"
}

// Empty `HList` value.
case object HNil extends HNil

解决方法

(只是猜测,我不知道原因.)

也许是因为类型_0可用(如def fun [N<:Nat] = ???; fun [_ 0],或更简单地定义有关此类型的含义). 如果_0将是单例,则只有类型_0.type可用. 和HNil一样吗?

(编辑:李大同)

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

    推荐文章
      热点阅读