Given the following Scala code:
def stream(callback: (String, Array[Byte]) => Unit) {
callback("application/json;charset=utf-8", Array(1))
callback("application/xml;charset=utf-8", Array(1))
}
@Test
def whenStreaming_thenShouldSeeSameMessages() {
var actualMessages = List.empty[(String, Array[Byte])]
sut.stream {
(contentType, payload) =>
actualMessages += (contentType, payload)
}
expect(List("application/json;charset=utf-8", "application/xml;charset=utf-8")) {
actualMessages
}
}
Why am I getting the following error message:
error: too many arguments for method +: (other: String)java.lang.String
actualMessages += (contentType, payload)
Specifically, payload is highlighted in IDEA, and on the command line, += is highlighted. I really don’t understand that error message.
The method to create a new list with an appended element is actually
:+. To update the listvarwith an appended version, you need to do,Or you can prepend with,
(For big lists, prepending is actually preferable, as it’s O(1) compared to O(n) for appending. One strategy is to use prepending to build the list in reverse, and then at the end call
List.reverse.)With the
+operator you’re trying to use, Scala thinks you want to do string concatenation,This is an unfortunate carry-over from Java. Generic string concatenation is implemented in Scala using an implicit conversion defined in scala.Predef,
What’s happening is that
Listdoes not implement a+method, so the compiler looks for a conversion to something that does provide+, and findsany2stringadd. This is how, e.g.,true + " false"works.