Import package as other

Let's say I have a Scala project with a bunch of case classes in the com.example.abc package. I want to import all these classes into the com.example.c package (which contains several non-conflicting case classes), so in any other place in my project I only need to import com.example.c._ use each case class as from com.example.c and com.example.abc .

How can i do this?

+6
source share
2 answers

Discussing the addition of an export mechanism that will do what you want, but it is not clear whether this will happen.

In any case, the only way so far is

  • Define the type of each class
  • Set val value equal to each object

So for example

 package bar case class Foo(i: Int) {} 

will need to mimic another package using

 package object baz { type Foo = bar.Foo val Foo = bar.Foo } 

When faced with this, people usually simply agree to an additional import or two.

+8
source

The import statement in scala simply tells the compiler where to find other classes, such as java, and not as the #include directive in C / C ++, where the compiler physically copies the entire header file. If you want to use case classes from com.example.abc , you must import them from your own package, as this is the usual way.

+1
source

All Articles