Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is wildcard import bad in Scala with respect to incremental compilation?

In Scala, is it bad, from the point of view of efficacy and speed of incremental compilers (sbt, sbt in Eclipse, IntelliJ), to use wildcard imports? Does it adversely affect the way these incremental compilers decide what to recompile in case of changes?

For instance, if for a new class X, I would only need to import classes A and B (and not C) from package pack, do I get a penalty for writing this:

import pack._

instead of this?

import pack.{ A, B }

Assuming A and B have no dependency on C, would X be recompiled with the wildcard import and not with the more specific import when C changes, or would the dependency tracking system be smart enough to realize that C is not used by X despite the wildcard import?

like image 694
Jean-Philippe Pellet Avatar asked Jul 02 '12 10:07

Jean-Philippe Pellet


People also ask

Do wildcard imports affect performance?

There's no performance impact to using the wildcard since the compiler will only import the classes required by your code.

Why is it better to avoid * in import statements in Java?

Arraylist in the . class file. Without the wildcard(*), the directive tells the compiler to look for one specific file in the classpath. Thus, it is suffice to say that the import directive may influence the compile time but it does not affect the runtime of the program.

Should we use * in import Java?

Java developers use the wildcard (*) in import statements to import all the classes in a particular package.


1 Answers

There is one tiny impact, but you probably won't notice it. The impact is that when there's a reference to symbol "Foo" the compiler must resolve "Foo" into a fully qualified name. The scope of where it can look for "Foo" is affected by wildcard imports. But that's all done in memory and you almost certainly won't notice such tiny differences in resolution speed unless you have something crazy like thousands of classes in one package.

Other than that, no impact. If you import pack._ and some arbitrary class in pack._ that you don't depend on changes then your file won't have to be recompiled.

like image 137
James Iry Avatar answered Sep 18 '22 12:09

James Iry