为什么使用 Foo[T <: Bar] 需要 Foo[_ <: Bar] 而不是 Foo[_] [英] Why does usage of Foo[T <: Bar] require Foo[_ <: Bar] rather than Foo[_]
问题描述
给定
trait Foo[T <: Bar] {
def foo: T
}
为什么编译器不像
def foo(f: Foo[_])
表示未知的_
必须是一种Bar
.相反,它迫使我这样做
to mean that the unknown _
must be a type of Bar
. Instead, it forces me to do this
def foo(f: Foo[_ <: Bar])
使签名复杂化.我确定编译器一定是正确的,我遗漏了一些微妙但很明显的 Foo[X]
其中 X
是 NOT 某种类型Bar
不可能通过构造实现,为什么?
which complicates signatures. I'm sure the compiler must be right and I'm missing some subtlety but clearly a Foo[X]
where X
is NOT some kind of Bar
so not possible by construction, so why?
谢谢.
推荐答案
简而言之 - 似乎是编译器的一个缺陷,是一个是否要解决它的悬而未决的问题.修复它可能会产生意想不到的后果,但它被频繁地提出,仍然被认真考虑作为一个可能的修复.以下是一些最相关且仍处于开放状态的 Bug 工单.
In short - seems to be a deficiency of the compiler and an open question whether to address it. Fixing it may have unintended consequences but it's been raised often enough to still be considered seriously as a possible fix. Here are some of the most relevant and still open Bugs tickets.
P.S.似乎 Dotty 确实尊重这样的界限,所以他们很可能会修复它!
- https://github.com/scala/bug/issues/11491- 2019 年 4 月 17 日起
- https://github.com/scala/bug/issues/8198- 从 2014 年 1 月 28 日起
- https://github.com/scala/bug/issues/1786- 从 2009 年 3 月 11 日起
- https://github.com/scala/bug/issues/11491 - from Apr 17, 2019
- https://github.com/scala/bug/issues/8198 - from Jan 28, 2014
- https://github.com/scala/bug/issues/1786 - from Mar 11, 2009
这篇关于为什么使用 Foo[T <: Bar] 需要 Foo[_ <: Bar] 而不是 Foo[_]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!