7 Uses of Underscore in Scala
Underscores are overloaded in Scala: see 7 places where '_' means different things.
Join the DZone community and get the full member experience.Join For Free
This article is for the curious Scala programmer who has seen underscores more times than they'd find comfortable. In this article I'll share almost all the places where you might see an underscore, so that you don't freak out when you see yet another one in a different context.
Other than that, I want to get straight to the point.
Underscores are commonly used when names or values are not important and/or unused. Here is a comprehensive list of ignored elements. First, variable names:
Why you'd do that, I have no idea - the point of intentionally declaring a value or variable is to use it. But it's possible. Probably the most useless case of an underscore. However, the case when the compiler throws a value at use, but we don't use it, so we use an underscore. The most common use case is with lambdas whose arguments we don't need or use:
We could have said
(1 to 10).map(x => 5) but we don't need x and the compiler would have issued an unused variable warning anyway, so we replace with an underscore.
Another scenario is when we use self-types as a type restriction, but you don't actually need to name the self-type:
Finally, one of the less common places where underscores ignore stuff is generic types. For example:
Since we don't care what type those Options are, we type them with underscore. Underscores are also useful when we want to interoperate with Java libraries or generic types which were not explicitly typed (think pre-Java 5).
Underscores are also used to have the meaning of "everything" in certain contexts. One of them is importing everything in a package:
But by far the most frequent use case is in pattern matching, where the underscore means "match everything".
3. Default Initializers
Particularly for variables, when we don't know what to initialize them with, let the JVM decide - zero for numerical values, false for Booleans, null for reference types:
4. Lambda Sugars
You've probably seen this before. Let's say we want to multiply every element of a list by 5, we would do
Except there's an even shorter version. We can wrote
which means the same thing: the compiler expands that to the slightly longer lambda version. The downside is that we can only use the underscore once in the function body - that's because each underscore represents a different argument of the lambda. For example, if I wrote
_ + _ part is identical to
(a, b) => a + b. Java programmers seeing this for the first time might find it too short, but it's really easy to get used to it.
Underscores are also used to turn methods into function values. We talk about the process in detail in another article (check the link above).
The underscore is a signal to the compiler to create a new lambda with the implementation of
x => incrementer(x).
6. Higher-Kinded Types
HKTs are generic types, whose type arguments are themselves generic. Libraries like Cats exploit this like crazy. Their structure is of the form
Where the type argument M is also generic. If I want to instantiate my HKT with a concrete generic type like List, I'll use the List type (not a List[String] or something else):
7. Vararg methods
When we have a method taking a variable number of arguments, such as
we might find ourselves we need to expand some collection of Strings as arguments to this method. We can't pass them manually at runtime, so we need to expand the collection automatically:
In this way, the elements of the list are exploded into the argument list to the method.
Scala overloaded the meaning of the underscore in many, many places - hopefully this article served as a good overview of where you might see it in real code.
Let me know if I've missed anything!
Published at DZone with permission of Daniel Ciocirlan. See the original article here.
Opinions expressed by DZone contributors are their own.