Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala import multiple packages

Tags:

package

scala

I've got some Scala classes and objects, in their own packages. Each package has a package object defining some implicits, so for example import com.foo.bar._ imports all implicits and classes from the bar package

What I'd like to know is, is there a way to define an "umbrella" import, say com.foo.all, such that

import com.foo.all._

is equivalent to

import com.foo.bar._
import com.foo.baz._
import com.foo.qux._
...

I can understand that this might be a little unclear, but if I we consider the case where I have a large number of my own packages, this would clearly be more concise.

like image 710
paulmdavies Avatar asked Feb 18 '13 11:02

paulmdavies


2 Answers

The Scala way to import multiple classes/objects from the same package is:

import scala.collection.immutable.{Map, HashMap, etc}

Cool Scala only trick: aliasing/renaming

import java.util.{Collection => JavaCollection}
like image 65
flavian Avatar answered Oct 22 '22 19:10

flavian


The shortest form I can come up is:

import com.foo._, bar._, baz._, qux._

For example:

import scala.collection.mutable._, PriorityQueue._, ArrayBuffer._

EDIT

Perhaps, you want to use only some particular things, as @alex23 pointed out, so you may write:

import com.foo.{foo, bar, baz}, bar._, baz._, qux._
like image 44
om-nom-nom Avatar answered Oct 22 '22 19:10

om-nom-nom