I have a method that uses foldLeft in Scala.
def bitSetToByte(b:collection.BitSet, sh:Int=0) =
((0 /: b) {(acc, input) => acc + (1 << (input - sh))}).toByte
The method has two parameters for the anonymous function, so I replaced it with _ by removing formal arguments.
def bitSetToByte(b:collection.BitSet, sh:Int=0) = ((0 /: b) {(_ + (1 << (_ - sh))}).toByte
The issue is that I have type mismatch error message.
What might be wrong?
When interpreting _
, the compiler assumes that the anonymous function that the _
corresponds to is enclosed in the nearest set of parentheses (except for (_)
). Basically, the compiler interpreted (_ - sh)
to be (x => x - sh)
, then complained because you were passing a function to <<
when it expected an Int
.