When I read the book 《Functional Programming in scala》. I find the expression like this:
case (Cons(h, t), Empty) =>
Some(f(Some(h()), Option.empty[B]) -> (t(), empty[B]))
What's the difference between
Some(f(Some(h()), Option.empty[B]), (t(), empty[B]))
If your 2nd example compiles, it should compile with a warning: creating a 2-tuple: this may not be what you want
Otherwise it would fail because Some()
doesn't take two parameters. The 1st example should compile because the ->
is explicitly creating the tuple to send as a single parameter to the (outer) Some()
.
When creating a tuple of two elements you have the option of using parentheses and comma (5, true)
, or the arrow 5 -> true
. In most situations the parentheses are optional when using the arrow version.
The arrow can't be used if you want more than 2 elements (ie not nested tuples):
'c' -> 'b' -> 'x'
//res0: ((Char, Char), Char) = ((c,b),x)
The ->
is actually a method of the ArrowAssoc class to which every object can be implicitly converted. See the object scala.Predef
. It is defined as:
def -> [B](y: B): Tuple2[A, B] = Tuple2(x, y)
This means that 1 -> 2
is equivalent to 1.->(2)
which evaluates to Tuple2(1, 2)
. This is also explained in section 21.4 of the book (3rd edition).
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.