Looking at the following Scala in Depth code, what does handle -> callback
mean?
trait Observable {
type Handle
def observe(callback: this.type => Unit): Handle = {
val handle = createHandle(callback)
callbacks += (handle -> callback)
handle
[code omitted]
}
->
ultimately comes from scala.ArrowAssoc . scala.Predef also defines:
@inline implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x)
which causes an implicit conversion of Any to an ArrowAssoc. This has the effect of adding ArrowAssoc's ->
method to all objects (because all objects inherit from Any).
As to what ->
does , it merely returns a Tuple2 of the two parameters. Or to put it another way, (handle -> callback)
is more or less identical to (handle, callback)
.
Although you've deleted the definition, callbacks
is probably a mutable.Map
object. +=
adds (or updates) an entry in said map associating handle
to callback
. So basically, this appears to be adding the newly-created handle and the callback to a map of callbacks.
Handle is an "abstract type". It must be defined in a concrete subclass.
callback is a function that takes a (this.type) and returns nothing (Unit).
The -> syntax is just alternate syntax for a tuple:
(3 -> "abcd") is the same as (3, "abcd")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.