全局/本地命名冲突的解决方法
Workarounds for a global / local naming conflicts
假设我有这两个 classes:
class A {
def foo(i: String) = println(s"${i}")
def foo(i: String, j: Int) = println(s"${i} ${j}")
}
class B {
def foo(i: Int) = println(s"${i}")
def foo(i: Int, j: String) = println(s"${i} ${j}")
}
我有一个与全局变量和方法同名的变量 class:
val inst = A()
abstract class AppB extends App {
def inst = B()
}
我扩展上面的内容:
object MyApp extends AppB {
// Should reference A
inst.foo("s11")
inst.foo("s22", 11)
// Should reference B
inst.foo(33)
inst.foo(44, "s33")
}
假设我不想重命名,有没有办法:
- 引用全局
val inst
?
- “影子”
def inst
不知怎么的,让我只能使用全局val inst
一个?
来自https://dotty.epfl.ch/docs/reference/dropped-features/package-objects.html:
If a source file src.scala
contains such top-level definitions, they will be put in a synthetic object named src$package
也许在这种情况下使用“自动生成的”object
名称访问 val inst
可行?
假设我有这两个 classes:
class A {
def foo(i: String) = println(s"${i}")
def foo(i: String, j: Int) = println(s"${i} ${j}")
}
class B {
def foo(i: Int) = println(s"${i}")
def foo(i: Int, j: String) = println(s"${i} ${j}")
}
我有一个与全局变量和方法同名的变量 class:
val inst = A()
abstract class AppB extends App {
def inst = B()
}
我扩展上面的内容:
object MyApp extends AppB {
// Should reference A
inst.foo("s11")
inst.foo("s22", 11)
// Should reference B
inst.foo(33)
inst.foo(44, "s33")
}
假设我不想重命名,有没有办法:
- 引用全局
val inst
? - “影子”
def inst
不知怎么的,让我只能使用全局val inst
一个?
来自https://dotty.epfl.ch/docs/reference/dropped-features/package-objects.html:
If a source file
src.scala
contains such top-level definitions, they will be put in a synthetic object namedsrc$package
也许在这种情况下使用“自动生成的”object
名称访问 val inst
可行?