Scala 3 匹配类型和 F-Bounded 类型:“无法证明”

问题描述 投票:0回答:1

鉴于:

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

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

我做错了什么?

斯卡斯蒂链接

scala type-level-computation scala-3 match-types
1个回答
0
投票

你没有做错任何事,这是一个实施限制。

您的示例自 Scala 3.4 起就可以成功编译,这要归功于 SIP-56 中引入的更改:“匹配类型的正确规范”(textPR)。

© www.soinside.com 2019 - 2024. All rights reserved.