为什么两个类型的别名没有具有相同名称的扩展方法?
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论