Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize imports in a Scala project?

Tags:

import

scala

Suppose my project has package org.abc with several Scala files inside. All the Scala files contain the same import statements, e.g. import org.x._, import org.y._, import org.z._

I don't like the repetition. Can I move all these import statements to one single file ?

like image 817
Michael Avatar asked May 06 '17 16:05

Michael


2 Answers

You can use the same pattern that is used in scala/package.scala: create an org.abc package object with a type and a val for each definitions you want to import from org.{x, y, z}._:

package org

package object abc {
  type Maybe[A] = org.x.Maybe[A]
  val Maybe = org.x.Maybe
  type MyClass = org.y.MyClass
  val MyClass = org.y.MyClass
}
like image 144
OlivierBlanvillain Avatar answered Sep 24 '22 08:09

OlivierBlanvillain


yes you can, by using package objects

like image 23
pedrorijo91 Avatar answered Sep 26 '22 08:09

pedrorijo91