【问题标题】:Why does this match type employing object types equate to Nothing?为什么这种使用对象类型的匹配类型等于 Nothing?
【发布时间】:2021-11-11 00:43:56
【问题描述】:

在 Dean Wampler 的书中,Programming Scala, 3rd Edition,有一个 example 与:

type Elem[X] = X match
  case String => Char
  case IterableOnce[t] => t
  case Array[t] => t
  case ? => X 

summon[Elem[List[Int]] =:= Int]
summon[Elem[Nil.type] =:= Nothing] 

似乎没有解释,至少在周围的上下文中,为什么summon[Elem[Nil.type] =:= Nothing] 而不是summon[Elem[Nil.type] =:= Nil.type]。为什么会这样?

【问题讨论】:

  • case IterableOnce[t] => t
  • 啊,对了,我忘记了 None extends Option[Nothing],例如,Nil extends List[Nothing]

标签: scala scala-3 match-types


【解决方案1】:

如 cmets 中所建议,Nil 扩展 List[Nothing],因此您的 case IterableOnce[t] => t 适用和 Elem[Nil.type] =:= Nothing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多