Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Where should I place import statements?

Tags:

Scala allows to import almost whatever you want, wherever you want, and this is great. But are there any considerations I should take into account when importing something inside class, method or just any block? How does it relate to performance, style, maintainability of code, etc?

Generally I try to obey these rules (made up by myself):

  • If I'm importing something external from other package I always place it at the top just after the "package".
  • If I'm using something more than once in the same file, I also import it at the top.
  • Otherwise I place my imports at the top of the relevant class/trait/object.
  • I avoid to import things in methods and blocks.
  • I try to avoid importing contents of instance objects, unless I have a really good reason to do so.
  • I would avoid renaming and "hiding" unless to resolve name collisions, but I have never needed that yet.

Do those "rules" make sense to you? Am I restricting myself too much?

like image 500
Vilius Normantas Avatar asked Apr 24 '11 10:04

Vilius Normantas


People also ask

Where do you put the import statements in Java?

In Java, the import statement is written directly after the package statement (if it exists) and before the class definition.

What does import do in Scala?

Importation in Scala is a mechanism that enables more direct reference of different entities such as packages, classes, objects, instances, fields and methods. The concept of importing the code is more flexible in scala than Java or C++. The import statements can be anywhere.

How do I import shells to Scala?

You need to run console from within the sbt shell. Then, sbt will compile whatever is defined in funsets and automatically add it in your Scala shell. From there, you'll be able to do import funsets. _ .

Where are Scala packages?

Packages are declared as a first statement at the top of a Scala file.


2 Answers

It usually makes sense to restrict the scope of something (e.g. a variable or a method) to the "least" as it is possible. For example, use an inner def as opposed to one at the class level. Why should import statements be any different? Why pollute a class with imports which are only used in a single block?

I like to declare imports as close to where they are used as possible!

The upshot of this is that common utilities, both libraries like scalaz and my own, tend to get imported once at the top-level (because they are used throughout the class). Whereas stuff like I/O gets imported locally, only where it is used

like image 60
oxbow_lakes Avatar answered Oct 06 '22 00:10

oxbow_lakes


Put imports at the top of a file.

Having them scattered all over a file makes it harder to read the code:

  • They convey no logical meaning, they're merely an alias; thus, they "pollute" the code which reflects the logical aspect of the program.
  • One cannot expect where to find them (this is what conventions are for).
  • In files with multiple references to entities with the same name but a different namespace, it's hard to keep track of what that name references in every scope.

I see no benefit in placing them next to their closest scope. Actually, with this reasoning in mind, one should never use them at all; instead, one should always use the full namespace for every referenced entity. Does that make any sense? IMHO, no.

like image 27
Eyal Roth Avatar answered Oct 05 '22 23:10

Eyal Roth