Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - tuple type inference fail? [duplicate]

Possible Duplicate:
Adding a tuple to a set does not work

I have this code:

class A

var buffer = Buffer[(A, Int)]()

then, somewhere:

val a = new A
buffer += (a, 0) // error

the type inferencer fails on a in (a, 0) telling that I pass A when I must pass (A, Int):

scala> def make {
     | val a = new A
     | buffer += (a, 0)
     | }
<console>:11: error: type mismatch;
 found   : A
 required: (A, Int)
       buffer += (a, 0)
              ^

however if I do this:

val a = new A
val tuple = (a, 0)
buffer += tuple

the error is gone. Is this some kind of bug or am I missing something?

like image 280
noncom Avatar asked Feb 16 '26 01:02

noncom


1 Answers

The compiler does not know, whether you mean buffer.+=(a,0) or buffer += Tuple2(a,0) here.

possible solutions:

buffer += Tuple2(a, 0)

buffer += Pair(a, 0) // Pair is an alias for Tuple2

buffer += ((a, 0))

buffer += a -> 0
like image 104
drexin Avatar answered Feb 19 '26 06:02

drexin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!