Reputation: 2596
I came out to scala programmin from Java so it's not clear to me how we should use relative imports in Scala and what is the exact name lookup rules? Suppose I have the following:
pack.age
|
|----MyClass.scala
com.age
|
|---AnotherClass.scala
I need to import MyClass.scala
into AnotherClass.scala
. Since Scala supports only relative imports I wrote the following:
import _root_.pack.age.MyClass
and it worked fine. But when I tried to delete _root_
from there, there was no compile time error either.
import pack.age.MyClass
works fine as well.
So, what is the package name lookup rules in Scala?
Upvotes: 2
Views: 172
Reputation: 1766
All imports are relative, so sometimes collisions can appear. For example, if you have package com.org.project.scala
then next import scala._
looks up for system package too. _root_
is implicit top package that can be utilised for simulation of absolute path.
Upvotes: 3
Reputation: 3455
I believe there is an order of operations here. If you had package.age.MyClass within com.age (ie. com.age.package.age.MyClass), as well as package.age.MyClass, the former would be picked up. If you wanted the latter, you would need to use the root syntax.
As there is only one place this class can be picked up from (in root), that is the package picked up.
Upvotes: 3