Scala Copy() Odd Behavior
Asked Answered
T

1

9

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?

Tungstate answered 13/11, 2010 at 20:24 Comment(1)
was just going to ask this question for scala 2.11 as well. glad I saw the answer below.Thor
E
13

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
       ^
Erepsin answered 13/11, 2010 at 20:39 Comment(1)
Another workaround would simply be to put a spaces around your equals, so: value = -7Collotype

© 2022 - 2024 — McMap. All rights reserved.