Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala - add a tuple to listBuffer

Tags:

scala

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
like image 205
Haiyuan Zhang Avatar asked Oct 28 '14 11:10

Haiyuan Zhang


1 Answers

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.

like image 198
The Archetypal Paul Avatar answered Oct 16 '22 19:10

The Archetypal Paul