Lets say I have in the same file:
object x {
case object z
}
object y {
.. //want to internally refer to x.z as xxx.z
}
In other words I need to rename an import of an object in the same file.
currently I resort to:
object y {
import x
val xxx=x
.. //referring to x.z as xxx.z
}
is there a better way of doing this? perhaps something along the lines of:
import .{x->xxx}
In Scala, you can rename imports like this:
import package.{x => xxx}
See here for more details: http://blog.bruchez.name/2012/06/scala-tip-import-renames.html
EDIT: from comment below
If you are in the same file - you don't need to import anything:
object X {
val a = 5
}
object Y {
val x = X
def p = println(x.a)
}
Y.p
Outputs
5
Yes, you can do it like this:
package packagename
object x {
case object z
}
object y {
import packagename.{x => xxx}
xxx.z
}
Note that you cannot import something that resides in the default package.
I came to the conclusion that for the proposes of what I was trying to do here, package object were really more appropriate as x only plays the role of organising the hierarchy. See here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.