首页 > 解决方案 > Scala 3 匹配类型和 F 有界类型:“无法证明”

问题描述

鉴于:

abstract class Quantity[A <: Quantity[A]]
sealed trait UnitOfMeasure[A]

class Time extends Quantity[Time]
object Minutes extends UnitOfMeasure[Time]

class PowerRamp extends Quantity[PowerRamp]
object KilowattsPerHour extends UnitOfMeasure[PowerRamp]
  
type Test[X <: UnitOfMeasure[?]] = X match
  case UnitOfMeasure[t] => t

以下编译:

summon[Test[Minutes.type] =:= Time]
summon[Test[KilowattsPerHour.type] =:= PowerRamp]

但是,如果UnitOfMeasure声明了 trait:

sealed trait UnitOfMeasure[A <: Quantity[A]]

两次召唤都失败了:

Cannot prove that Test[Minutes.type] =:= Time.

Note: a match type could not be fully reduced:
  trying to reduce Test[Minutes.type]
  failed since selector Minutes.type
  matches none of the cases
    case UnitOfMeasure[t] => t

我究竟做错了什么?

斯卡斯蒂链接

标签: scalatype-level-computationscala-3match-types

解决方案


推荐阅读