为什么使用 Foo[T <: Bar] 需要 Foo[

编程入门 行业动态 更新时间:2024-10-27 03:28:48
本文介绍了为什么使用 Foo[T <: Bar] 需要 Foo[_ <: Bar] 而不是 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 确实尊重这样的界限,所以他们很可能会修复它!

  • github/scala/bug/issues/11491- 2019 年 4 月 17 日起
  • github/scala/bug/issues/8198- 从 2014 年 1 月 28 日起
  • github/scala/bug/issues/1786- 从 2009 年 3 月 11 日起
  • github/scala/bug/issues/11491 - from Apr 17, 2019
  • github/scala/bug/issues/8198 - from Jan 28, 2014
  • github/scala/bug/issues/1786 - from Mar 11, 2009

更多推荐

为什么使用 Foo[T &lt;: Bar] 需要 Foo[

本文发布于:2023-10-16 15:53:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:amp   Foo   Bar   lt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!