Using Wildcard Import in Java and Scala

I have recently heard statements like “you should never use wildcard imports” too often. Therefore, I want to talk about this community. If importing templates will never be used in Java code, whatever happens? Are there exceptions to this rule? I am interested in your personal experience and opinion. Do you use them in your production code and would you recommend it to others? How do you use them - can you recommend the best way to do this.

It is also interesting to look at it from the point of view of Scala. Does the same apply to Scala? Or should wildcard import in Scala be used only in presentation slides and SO responses?

If you look at the scalaz page, for example, they recommend using wildcard imports, for example:

import scalaz._ import Scalaz._ 

I think it’s also important to consider implicit conversions that are usually imported using wildcards.

+8
java import scala wildcard
source share
4 answers

In Scala, importing wildcards is mandatory because many libraries expect their implicit conversions to be in scope, but they are not always convenient to name. Thus,

 import collection.JavaConversions._ 

is a great idea, whereas

 import collection.JavaConversions.{asJavaConcurrentMap,enumerationAsScalaIterator,...} 

incredibly uncomfortable. Even better, in Scala you can place your import in any area:

 package mypackage { class MyClass { def myGraphicalWidgetHandler { import java.awt._ ... } ... } ... } 

which really helps to keep the namespace clutter in the whole file. And you can selectively rename parts of the import that, as you know, will conflict:

 import java.awt.{List => AwtList, _} 

Unlike Java, you are limited to the global import area, and you cannot rename them; you also do not have implicit transformations, so it’s good to only pull the things you are looking for. On the other hand, you have powerful IDE support to help you find the class you are looking for and import just that for you. So for Java, there is a reasonable argument that you should let your IDE use only what you need and not solve everything. Personally, I still find this too inconvenient and just use the import of wildcards most of the time.

+12
source share

Well, by giving full class names, you eliminate ambiguity. Therefore, when you explicitly specify which class to import, it is much easier to understand the intent of the code. Java 1.2 also comes to mind:

 import java.util.*; import java.awt.*; ... List blah; 

This worked fine in Java 1.1. However, in Java 1.2, the List interface was added to java.util, and the code that was in order no longer worked. Many developers cried.

+12
source share

In Java, using wildcards for import or not, it is mainly about maintainability of the code and [un-] willingness to deal with import ambiguities (when two imported packages have members with the same name). On the other hand, from an ideological point of view, it makes sense to import the entire package (say java.sql._ ), you want to have consistent behavior and avoid multiple import lines from the same package.

Most of them relate to Scala, with the difference that:

  • If you want to import several members from the same class without polluting the code, and at the same time avoid possible ambiguities, Scala offers a special syntax for this: import java.io.{File, FileInputStream} ;
  • In Scala, you can give aliases to imported members to resolve ambiguities: import java.lang.{Double=>JDouble} ;
  • As you correctly pointed out, using the import of wildcards, you add implications to the context, which can lead to a different level of ambiguity (so there’s another reason to think twice);

So, in general, IMO, the syntax for importing wildcards into Scala should be used only if you are working with a specific library and want it to work sequentially (in the case of Scalaz, to have all the required members, implicit conversion, etc. .).

+8
source share

For the Java side: there is absolutely nothing wrong with using wildcard imports! There is no performance at run time because only the classes used are loaded.

The java import mechanism occurs at compile time. The only thing it uses is if you use the Date class, for example, in your ant code there is no Date class in the same package, the import mechanism will be used to find the Data class in one of the imports.

So all he does is "find out what class you are referring to." Nothing that can change your performance at runtime.

+2
source share

All Articles