Search code examples
scalaarraybuffer

Fill ArrayBuffer with pairs of Double


I need some guidance here, please.

What I have:

import scala.collection.mutable.ArrayBuffer
var buffer = ArrayBuffer.empty[(Double, Double)]

and I want to fill the buffer with pairs. I'm trying this but it doesn't work:

for(someCycle){
    buffer += (someDouble, someOtherDouble)
}

the error:

 error: type mismatch;
 found   : Double
 required: (Double, Double)
              buffer += (someDouble, otherDouble)

I understand the error but I can't figure out the right syntax.


Solution

  • Since += is a function, the compiler is inferring it as:

    buffer.+=(someDouble, someOtherDouble)
    

    Making it think you're trying to pass two arguments to += instead of one (the error message is a bit misleading).

    You need an additional parenthesis:

    buffer += ((someDouble, someOtherDouble))
    

    Or alternatively:

    buffer += (someDouble -> someOtherDouble)