I am learning Scala and have a package with no dots in the name: test. I now want to import stuff from this package into another Scala file, but I
- do not want to use wildcard imports because I want to prevent name clashes (so no
import test._) - do not want to import individual classes (so no
import test.{My, Manually, Maintained, List, Of, Classes}) - do want to keep
testas a top level package (so nopackage nested.test) - would like to alias the package to a shorter name (something like
import {test => t})
I have played around but was not successful until now. I compile with scalac a.scala b.scala:
- file
a.scala
package test
class A {}
- experiments with file
b.scala, none of which did work:
import _root_.{test => t} // error: _root_ cannot be imported
class A extends t.A {}
import _root_.test // error: _root_ cannot be imported
class A extends test.A {}
import test // error: . expected
class A extends test.A {}
import test.A // warning: imported `A` is permanently hidden by definition of class A
class A extends A {} // error: illegal cyclic reference involving class A
import {test => t} // error: identifier expected but '{' found.
// error: . expected
class A extends t.A {}
In python I could achieve what I want like this
import test as t
class A(t.A): pass
and in Haskell I could do
import qualified Test as T
type A = T.A
Question: Can something like this be achieved in Scala? What could I do to get close to this?