Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Copy() Odd Behavior

I'm experiencing an odd bit of behavior when I use the auto-generated copy() method that was added in Scala-2.8.

From what I've read, when you declare a given class as a case-class, a lot of things are auto-generated for you, one of which is the copy() method. So you can do the following...

case class Number(value: Int)
val m = Number(6)

println(m)                     // prints 6

println( m.copy(value=7) )     // works fine, prints 7

println( m.copy(value=-7) )    // produces:  error: not found: value value

println( m.copy(value=(-7)) )  // works fine, prints -7

I apologize if this question has already been asked, but what is going on here?

like image 760
shj Avatar asked Nov 13 '10 20:11

shj


1 Answers

Scala allows many method names that other languages don't, including =-. Your argument is being parsed as value =- 7 so it is looking for a method =- on value which doesn't exist. Your workaround all change the way the expression is parsed to split up the = and the -.

scala> var foo = 10
foo: Int = 10

scala> foo=-7
<console>:7: error: value =- is not a member of Int
       foo=-7
       ^
like image 141
Ben Jackson Avatar answered Sep 23 '22 23:09

Ben Jackson