22

In my opinion, one of the greatest things about Scala is its interoperability with Java and its similar syntax. One thing that I found strange is the use of the _ operator for package wilcard imports instead of the * operator that is used in Java.

Is there a technical reason for using _ instead of *? If not, then why was this change done?

Peter Mortensen
1,0452 gold badges12 silver badges15 bronze badges
asked Apr 11, 2013 at 19:06
1
  • This might just be a functional thing. I know Haskell uses _ in a few places to act as a wildcard/not-specified value. Commented Apr 11, 2013 at 19:17

4 Answers 4

39

In Scala, the * is a valid identifier. One could write:

val * = "trollin'"
println(*)

With the result being:

trollin'

One could write a class named * as such:

class * {
 def test():String = {
 "trollin'"
 }
}

So with that being the case, when I have a class * in the package us.hexcoder and I write:

import us.hexcoder.*

You would be saying that you wish to import a class with the name *. Because of this, Scala needed to use another symbol to indicate a wildcard import. For whatever reason, they decided to use _ as the wildcard symbol.

answered Apr 11, 2013 at 19:30
5
  • 11
    The question becomes then, why was * chosen as a valid identifier and _ as wildcard? Commented Apr 11, 2013 at 20:01
  • 3
    Even though I have more upvotes, this is the more correct answer. Never heard of * as an identifier before. Commented Apr 11, 2013 at 20:12
  • 1
    @MikeBrown It's also valid in Lisp. Here is an example in Scheme Commented Apr 11, 2013 at 21:02
  • 20
    @MikePatridge Almost definitely because Scala defines all of its operators as functions, and allows the developer to define their own operators as such. It doesn't do special-case syntax for operators, and * needed to be included for multiplication. So another less common character needed to be chosen as a reserved character. Commented Apr 11, 2013 at 23:08
  • 3
    The _ character resembles a blank (as in "fill in the blank"), so whether Scala borrowed it from somewhere else or came up with it themselves, it makes sense to use it as a wildcard. Commented Apr 12, 2013 at 3:11
13

In functional languages, the _ character is commonly used to say, "I don't care about this parameter" or "anything can go here". Extending that value to namespace imports only makes sense.

answered Apr 11, 2013 at 19:17
2

In addition to Glenn's answer, import is a valid statement anywhere in Scala and you can import an object or an instance members into scope. As * is obviously a member of many classes, it can not be used as a wildcard for the import statement. So you end up with a string which must not be a valid identifier.

_ comes to mind. The fact that it is used in other places for a different meaning (existential type, function as an instance) is also due to the same issue.

answered Apr 12, 2013 at 1:25
0

In Scala there are dozens of places where the underscore is used. Using it for wildcard imports is just a logical consequence.

answered Apr 11, 2013 at 19:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.