为什么两个类型的别名没有具有相同名称的扩展方法?

发布于 2025-01-19 23:03:31 字数 890 浏览 5 评论 0原文

在Scala 3替补中,输入以下

type B = Int

extension (a: A)
  def f() = println("A!")

extension (b: B)
  def f() = println("B!")

给出错误,

8 |  def f() = println("B")
  |      ^
  |Double definition:
  |def f(a: A)(): Unit in object rs$line$1 at line 5 and
  |def f(b: B)(): Unit in object rs$line$1 at line 8
  |have the same type after erasure.
  |
  |Consider adding a @targetName annotation to one of the conflicting definitions
  |for disambiguation.

为什么我不能这样做?

我希望在以下内容中,类型定义足以使编译器/Scala知道要使用哪种实现

val a: A = 0
val b: B = 0
val x: Int = 0

a.f() // I would expect "A!"
b.f() // I would expect "B!"
x.f() // I would expect an error

我怀疑不透明类型的别名我在寻找什么?

In a Scala 3 REPL, entering the following

type B = Int

extension (a: A)
  def f() = println("A!")

extension (b: B)
  def f() = println("B!")

gives the error

8 |  def f() = println("B")
  |      ^
  |Double definition:
  |def f(a: A)(): Unit in object rs$line$1 at line 5 and
  |def f(b: B)(): Unit in object rs$line$1 at line 8
  |have the same type after erasure.
  |
  |Consider adding a @targetName annotation to one of the conflicting definitions
  |for disambiguation.

Why can't I do this?

I was hoping that in the following, the type definitions would be sufficient for the compiler / for Scala to know which implementation to use

val a: A = 0
val b: B = 0
val x: Int = 0

a.f() // I would expect "A!"
b.f() // I would expect "B!"
x.f() // I would expect an error

I suspect opaque type aliases are what I'm looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文