[英]Dotty cannot infer result type of generic Scala function taking type parameter trait with abstract type
Imagine this simple trait Value
where every implementing class has a value
of some type T
.想象一下这个简单的 trait Value
,其中每个实现类都有一个T
类型的value
。
trait Value {
type T
def value: T
}
We have two different implementing classes representing Int
and String
values respectively.我们有两个不同的实现类,分别表示Int
和String
值。
case class IntValue(override val value: Int) extends Value {
override type T = Int
}
case class StringValue(override val value: String) extends Value {
override type T = String
}
If we have a List
of values we would like to have a type safe way of selecting all values of a specific type.如果我们有一个值List
,我们希望有一种类型安全的方式来选择特定类型的所有值。 Class Values
and its companion object help us doing that:类Values
及其伴随对象帮助我们做到这一点:
object Values {
private type GroupedValues = Map[ClassTag[_ <: Value], List[Value]]
def apply(values: List[Value]): Values = {
val groupedValues: GroupedValues = values.groupBy(value => ClassTag(value.getClass))
new Values(groupedValues)
}
}
class Values private (groupedValues: Values.GroupedValues) {
// Get a List of all values of type V.
def getValues[V <: Value : ClassTag] = {
val classTag = implicitly[ClassTag[V]]
groupedValues.get(classTag).map(_.asInstanceOf[List[V]]).getOrElse(Nil)
}
def getValue[V <: Value : ClassTag] = {
getValues.head
}
def getValueOption[V <: Value : ClassTag] = {
getValues.headOption
}
def getValueInner[V <: Value : ClassTag] = {
getValues.head.value
}
}
All this works fine in both Scala 2.13 and Dotty 0.20.0-RC1 so having a list of mixed values…所有这些在 Scala 2.13 和 Dotty 0.20.0-RC1 中都可以正常工作,因此有一个混合值列表......
val valueList = List(IntValue(1), StringValue("hello"))
val values = Values(valueList)
…we can select elements and get them returned as the correct type – all checked at compile-time: ...我们可以选择元素并将它们作为正确的类型返回——所有这些都在编译时检查:
val ints: List[IntValue] = values.getValues[IntValue]
val strings: List[StringValue] = values.getValues[StringValue]
val int: IntValue = values.getValue[IntValue]
val string: StringValue = values.getValue[StringValue]
val intOption: Option[IntValue] = values.getValueOption[IntValue]
val stringOption: Option[StringValue] = values.getValueOption[StringValue]
val i: Int = values.getValueInner[IntValue]
val s: String = values.getValueInner[StringValue]
Option[T]
fails in Dotty在 Dotty 中选择一个值作为Option[T]
失败However, if we add this function to select values as their T
type (ie Int
and String
) and get it returned as an Option
…然而,如果我们添加这个函数来选择值作为它们的T
类型(即Int
和String
)并让它作为一个Option
返回......
class Values ... {
...
def getValueInnerOption[V <: Value : ClassTag] = {
getValues.headOption.map(_.value)
}
}
…then things work fine in Scala 2.13: ...然后在 Scala 2.13 中一切正常:
val iOption: Option[Int] = values.getValueInnerOption[IntValue]
val sOption: Option[String] = values.getValueInnerOption[StringValue]
But in Dotty 0.20.0-RC1 this does not compile:但在 Dotty 0.20.0-RC1 中,这不会编译:
-- [E007] Type Mismatch Error: getValue.scala:74:29
74 | val iOption: Option[Int] = values.getValueInnerOption[IntValue]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Found: Option[Any]
| Required: Option[Int]
-- [E007] Type Mismatch Error: getValue.scala:75:32
75 | val sOption: Option[String] = values.getValueInnerOption[StringValue]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Found: Option[Any]
| Required: Option[String]
We can fix the problem by adding a type parameter to getValueInnerOption
that ties the return type and the abstract type T
together and allows us to specify the return type.我们可以通过向getValueInnerOption
添加一个类型参数来解决这个问题,该参数将返回类型和抽象类型T
在一起并允许我们指定返回类型。
def getValueInnerOption[V <: Value {type T = U} : ClassTag, U]: Option[U] = {
getValues.headOption.map(_.value)
}
Unfortunately, this means that we will have to add the actual type of T
(ie Int
or String
) at the call site which is a pity because it is just boilerplate.不幸的是,这意味着我们将不得不在调用站点添加T
的实际类型(即Int
或String
),这很遗憾,因为它只是样板文件。
val iOption: Option[Int] = values.getValueInnerOption[IntValue, Int]
val sOption: Option[String] = values.getValueInnerOption[StringValue, String]
It seems that Dotty already knows what the upper bound of T
is but cannot propagate that knowledge to the result type of the function.似乎 Dotty 已经知道T
的上限是什么,但无法将该知识传播到函数的结果类型。 This can be seen if trying to ask for a String
from an IntValue
:如果尝试从IntValue
请求String
,可以看到这IntValue
:
-- [E057] Type Mismatch Error: getValue.scala:75:39
75 | val wtf = values.getValueInnerOption[IntValue, String]
| ^
|Type argument IntValue does not conform to upper bound Value{T = String}
so is the original code (without type parameter U
) something that can be expected to work in the final Scala 3.0 or does it need to be written in a different way?那么原始代码(没有类型参数U
)是否可以在最终的 Scala 3.0 中工作,还是需要以不同的方式编写?
_.value
has a dependent function type which isn't inferred by default but you can specify it: _.value
有一个依赖函数类型,默认情况下不会推断出它,但您可以指定它:
def getValueInnerOption[V <: Value : ClassTag] = {
getValues.headOption.map((_.value): (v: V) => v.T)
}
and then进而
val iOption: Option[Int] = values.getValueInnerOption[IntValue]
val sOption: Option[String] = values.getValueInnerOption[StringValue]
compiles.编译。
But the problem is that I am not sure it (and getValueInner
) should work.但问题是我不确定它(和getValueInner
)是否应该工作。 Because the inferred return types for them involve V#T
(you can see them in an error message if you give wrong return type), and trying to specify them explicitly gives因为它们的推断返回类型涉及V#T
(如果您提供错误的返回类型,您可以在错误消息中看到它们),并且尝试明确指定它们会给出
V is not a legal path since it is not a concrete type V 不是合法路径,因为它不是具体类型
(see http://dotty.epfl.ch/docs/reference/dropped-features/type-projection.html ) (见http://dotty.epfl.ch/docs/reference/dropped-features/type-projection.html )
In Dotty trymatch types as a replacement for type projections在 Dotty 中尝试匹配类型作为类型投影的替代
type InnerType[V <: Value] = V match {
case IntValue => Int
case StringValue => String
}
trait Value {
type This >: this.type <: Value
type T = InnerType[This]
def value: T
}
case class IntValue(override val value: Int) extends Value {
override type This = IntValue
}
case class StringValue(override val value: String) extends Value {
override type This = StringValue
}
def getValueInner[V <: Value { type This = V } : ClassTag]: InnerType[V] = {
getValues.head.value
}
def getValueInnerOption[V <: Value { type This = V } : ClassTag]: Option[InnerType[V]] = {
getValues.headOption.map(_.value)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.