Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
This might just be a functional thing. I know Haskell uses _ in a few places to act as a wildcard/not-specified value. –  KChaloux Apr 11 '13 at 19:17

4 Answers 4

up vote 21 down vote accepted

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.

share|improve this answer
5  
The question becomes then, why was * chosen as a valid identifier and _ as wildcard? –  Mike Partridge Apr 11 '13 at 20:01
2  
Even though I have more upvotes, this is the more correct answer. Never heard of * as an identifier before. –  Mike Brown Apr 11 '13 at 20:12
    
@MikeBrown It's also valid in Lisp. Here is an example in Scheme –  Glenn Nelson Apr 11 '13 at 21:02
9  
@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. –  KChaloux Apr 11 '13 at 23:08
2  
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. –  Michael Shaw Apr 12 '13 at 3:11

In addition to Gleen'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. Indeed other places where it is used for a different meaning (existential type, function as an instance) is also due to the same issue.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.