Placeholder syntax limitations for parameters
scala
Solution
The spec is this section.
The intuition of "something complex" is the bit about "syntactic category Expr", as opposed to a "SimpleExpr", which you can review about half-way through the syntax section.
You can see there that the things inside parens are Exprs, so that's why people approximate the syntax by saying, "It expands to the innermost parens."
You can often avoid incurring an Expr by using infix notation. But your operator precedence has to help.
scala> (1 to 5) map ("x" * _)
res1: scala.collection.immutable.IndexedSeq[String] = Vector(x, xx, xxx, xxxx, xxxxx)
scala> (1 to 5) map ("x".*(_))
res2: scala.collection.immutable.IndexedSeq[String] = Vector(x, xx, xxx, xxxx, xxxxx)
scala> (1 to 5) map ("x".*(_ + 5))
<console>:8: error: missing parameter type for expanded function ((x$1) => x$1.$plus(5))
(1 to 5) map ("x".*(_ + 5))
^
scala> (1 to 5) map ("x" * _ + 5)
res5: scala.collection.immutable.IndexedSeq[String] = Vector(x5, xx5, xxx5, xxxx5, xxxxx5)
Compare with:
scala> (1 to 5) map ("abcdefg" apply _)
res8: scala.collection.immutable.IndexedSeq[Char] = Vector(b, c, d, e, f)
scala> (1 to 5) map ("abcdefg" apply _ + 1)
res9: scala.collection.immutable.IndexedSeq[Char] = Vector(c, d, e, f, g)
scala> (1 to 5) map ("abcdefg".apply(_ + 1))
<console>:8: error: missing parameter type for expanded function ((x$1) => x$1.$plus(1))
(1 to 5) map ("abcdefg".apply(_ + 1))
^
scala> (1 to 5) map ("abcdefg"(_ + 1))
<console>:8: error: missing parameter type for expanded function ((x$1) => x$1.$plus(1))
(1 to 5) map ("abcdefg"(_ + 1))
^
Problem
After having read the Programming in Scala book and searched a little, I still don't understand why this works: ``` val x = Array[Byte](1, 2, 3) x.map{Integer.toHexString(_)} ``` whereas this other slightly more complicated one doesn't: ``` val x = Array[Byte](1, 2, 3) x.map{Integer.toHexString((_ + 0x100) % 0x100)} ``` This longer alternative does work: ``` x.map{b => Integer.toHexString((b + 0x100) % 0x100)} ``` This is the obscure error message I get: ``` error: missing parameter type for expanded function ((x$1) => x$1.$plus(256)) ``` I'm using: - Exactly one _ for each existing parameter - Not using any inner anonymous function. Are the parenthesis harmful?