I've taken a look at the list of surveys taken on scala-lang.org and noticed a curious question: "Can you name all the uses of “_”?". Can you? If yes, please do so here. Explanatory examples are appreciated.
相关问题
- Unusual use of the new keyword
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
- Error in Scala Compiler: java.lang.AssertionError:
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- GRPC: make high-throughput client in Java/Scala
- Setting up multiple test folders in a SBT project
- Testing request with CSRF Token in Play framework
- Run project with java options via sbt
An excellent explanation of the uses of the underscore is Scala _ [underscore] magic.
Examples:
In Scala,
_
acts similar to*
in Java while importing packages.In Scala, a getter and setter will be implicitly defined for all non-private vars in a object. The getter name is same as the variable name and
_=
is added for the setter name.Usage:
If you try to assign a function to a new variable, the function will be invoked and the result will be assigned to the variable. This confusion occurs due to the optional braces for method invocation. We should use _ after the function name to assign it to another variable.
The ones I can think of are
Existential types
Higher kinded type parameters
Ignored variables
Ignored parameters
Ignored names of self types
Wildcard patterns
Wildcard patterns in interpolations
Sequence wildcard in patterns
Wildcard imports
Hiding imports
Joining letters to operators
Assignment operators
Placeholder syntax
Method values
Converting call-by-name parameters to functions
Default initializer
There may be others I have forgotten!
Example showing why
foo(_)
andfoo _
are different:This example comes from 0__:
In the first case,
process _
represents a method; Scala takes the polymorphic method and attempts to make it monomorphic by filling in the type parameter, but realizes that there is no type that can be filled in forA
that will give the type(_ => Unit) => ?
(Existential_
is not a type).In the second case,
process(_)
is a lambda; when writing a lambda with no explicit argument type, Scala infers the type from the argument thatforeach
expects, and_ => Unit
is a type (whereas just plain_
isn't), so it can be substituted and inferred.This may well be the trickiest gotcha in Scala I have ever encountered.
Note that this example compiles in 2.13. Ignore it like it was assigned to underscore.
From (my entry) in the FAQ, which I certainly do not guarantee to be complete (I added two entries just two days ago):
This is also part of this question.
Besides the usages that JAiro mentioned, I like this one:
If someone needs all connection properties, he can do:
If you need just a host and a port, you can do:
There is a specific example that "_" be used:
may be equal to :
Applying “_” in some scenarios will automatically convert to “(x$n) => x$n ”