cfi9
cfi9

Reputation: 1

Scala-Lang recommended import statement style: package._ or package.ClassName?

Looking for best Scala-Lang practice for import statements. Don't see any recommendation here:

  1. https://docs.scala-lang.org/style/naming-conventions.html#packages
  2. https://github.com/scala/scala

So what is recommended?

Option (1):

import package._

Option (2):

import package.ClassName

Upvotes: 0

Views: 125

Answers (2)

Mario Galic
Mario Galic

Reputation: 48410

Regarding implicits, wildcard imports have a potential advantage in that the name of the implicit can change without having to fix the import statements, for example, consider

object Qux {
  object Implicts {
    implicit class ExtraListOps[T](l: List[T]) {
      def shuffle: List[T] = scala.util.Random.shuffle(l)
    }
  }
}

import Qux.Implicts._
List(1,2,3,4,5).shuffle

Here we could rename ExtraListOps and yet would not have to modify import statement. On the other hand, Joshua Suereth states:

You can pull implicits in individually without wildcards. Explicit imports have 'priority' over wildcard imports. Because of this, you can shadow wildcard imports with an explicit import.

Also consider the discussion in the SIP Wildcard imports considered harmful, where for example, Ichoran states

Superfluous clutter at the top of every file that can only be managed by an IDE, and even then not very well, is something I find a lot more harmful in most cases. If you get conflicting implicits and the compiler doesn’t tell you exactly where they came from, that’s a compiler issue. Once detected, the fix is easy enough (e.g. import org.foo.{troublesomeImplicit => _, _}).

Upvotes: 1

Tim
Tim

Reputation: 27356

This is generally a matter of taste, but there is an argument for avoiding ._ so that the code is more robust to library changes that might introduce unwanted symbols into your code or create other subtle changes to your code that are difficult to track down.

Upvotes: 4

Related Questions