I'd like to add a three-integer tuple to a ListBuffer. intuitively, my first try is the code snippet 1, which has a syntax error,according to the compiler. Then, I've tried the snippet 2 and 3, both will work. So could someone explain to me why the code in snippet 1 syntactically wrong.
snippet 1
import scala.collection.mutable.ListBuffer
val b : ListBuffer[(Int, Int,Int)] = ListBuffer()
b += (1,1,1)
snippet 2
import scala.collection.mutable.ListBuffer
val b : ListBuffer[(Int, Int,Int)] = ListBuffer()
b += ((1,1,1))
snippet 3
import scala.collection.mutable.ListBuffer
val b : ListBuffer[(Int, Int,Int)] = ListBuffer()
val i = (1,1,1)
b += i
b += (1,1,1)
is interpreted as
b.+=(1,1,1)
which looks like a function call passing three parameters to +
Adding another pair of parens means it's interpreted as
b.+=((1,1,1))
which is passing the expected tuple.
Declaring the argument separately in
val i = (1,1,1)
also doesn't have that interpretation problem so is OK too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With